Ambiguous handler methods for http path?

2019-07-09 23:55发布

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!

1条回答
三岁会撩人
2楼-- · 2019-07-10 00:45

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.

查看更多
登录 后发表回答