Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous handler methods for http path?

I have a Spring application where I declared my class like so:

@Controller
@RequestMapping(value = "/rest/api/datasources/", produces = MediaType.APPLICATION_JSON_VALUE, method=RequestMethod.GET)
public class MetadataServiceController {
    //Two separate methods:
    @RequestMapping(value="{datasourceName}")
    public Object getLatestApiMetadata(@PathVariable String datasource, 
           @RequestParam (required = false)  String datasourceNum,
           @RequestParam (defaultValue = "true")  String dataFields, 
           @RequestParam ( required=false, defaultValue = "api")  String visibility){
      ... //Implementation here
    }

    @RequestMapping(value="{apiVersion}")
    public @ResponseBody List<DataSource> getAllMetadata(
        @RequestHeader(value="sub-version", required=false, defaultValue="0.0") String minorVer,
        @PathVariable String restApiVersion,
        @RequestParam(required = false) String datasourceNum,
        @RequestParam(defaultValue = "all") String visibility)
        throws ObjectNotFoundException {
    ... //Implementation here
    }

}

But when I try to reach one of these rest endpoints, I get an error saying: java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path and it specifies those two methods as the issue. I was under the impression that if I change the request parameters, Spring would not complain about them being the same via this post: http://www.coderanch.com/t/598675/Spring/handling-HTTP-Request-parameters but clearly it still does. Would anyone have any suggestions on how to get around this? Thanks!

like image 744
user1871869 Avatar asked Dec 10 '22 20:12

user1871869


1 Answers

What is important to Spring to dispatch the request is the Path portion of the URL.

Both request mappings capture any value placed in the path and it is impossible to distinguish which method should be invoked. In your example code, a request to www.example.com/rest/api/datasources/foo could be handled by getLatestApiMetadata where "foo" is the datasourceName and also handled by getAllMetadata where "foo" is the apiVersion.

like image 116
Tim Bender Avatar answered Dec 27 '22 21:12

Tim Bender