http路径的模糊处理程序方法? [英] Ambiguous handler methods for http path?

查看:30
本文介绍了http路径的模糊处理程序方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
    }

}

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

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!

推荐答案

Spring 发送请求的重要部分是 URL 的 Path 部分.

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

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

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.

这篇关于http路径的模糊处理程序方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆