java http 路径的处理程序方法不明确?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32837964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:51:28  来源:igfitidea点击:

Ambiguous handler methods for http path?

javaspringrestspring-mvc

提问by user1871869

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

我有一个 Spring 应用程序,我在其中声明了我的类:

@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 pathand 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-parametersbut clearly it still does. Would anyone have any suggestions on how to get around this? Thanks!

但是,当我尝试到达这些其余端点之一时,我收到一条错误消息:java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path并将这两种方法指定为问题。我的印象是,如果我更改请求参数,Spring 不会通过这篇文章抱怨它们相同:http: //www.coderanch.com/t/598675/Spring/handling-HTTP-Request-parameters但是显然它仍然如此。有人会对如何解决这个问题有任何建议吗?谢谢!

回答by Tim Bender

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

对于 Spring 分派请求来说,重要的是 URL 的 Path 部分。

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/foocould be handled by getLatestApiMetadatawhere "foo" is the datasourceNameand also handled by getAllMetadatawhere "foo" is the apiVersion.

两个请求映射都捕获放置在路径中的任何值,并且无法区分应该调用哪个方法。在您的示例代码中,请求www.example.com/rest/api/datasources/foo可以由getLatestApiMetadata“foo”所在的位置处理,也可以由“foo”所在的位置datasourceName处理。getAllMetadataapiVersion