微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring MVC 3.1 – 版本化REST Web服务

我想通过为不同版本提供不同的Accept标头值来对我的REST-Webservice进行版本控制(参见http://barelyenough.org/blog/2008/05/versioning-rest-web-services/).

问题是,Spring MVC 3似乎无法实现.

我的控制器看起来像这样:

@Controller
@RequestMapping("test")
public class RestController {

@RequestMapping(method = RequestMethod.GET,produces = "application/vnd.example.item-v1+json")
@ResponseBody
public ItemV1 getItem() {
    return new ItemV1();
}

@RequestMapping(method = RequestMethod.GET,produces = "application/vnd.example.item-v2+json")
@ResponseBody
public ItemV2 getItem2() {
    return new ItemV2();
}
}

当我尝试访问其中一种方法时,我得到一个异常:

java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path '/test'

我错过了什么,或者Spring MVC是不可能的?我知道JAX-RS有可能……

最佳答案
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-produces

这应该是你拥有东西的方式.你如何在GET请求中指定Accept标头?您是否100%肯定您的GET请求是否发送一个Accept标头值,该标头值仅匹配您指定的一种或另一种内容类型?如果你发送一个匹配两者的头,那么Spring将不知道哪个处理程序方法应该处理请求.

您可能需要将org.springframework日志记录发送到DEBUG以查看发生了什么,或者使用断点调试器和Spring源代码来查看实际发生的情况. “产生”是一个相对较新的功能,因此也有可能存在错误.

https://jira.springsource.org/secure/IssueNavigator.jspa?reset=true&jqlQuery=project+%3D+SPR+AND+%28summary+%7E+%22mvc+produces%22+OR+description+%7E+%22mvc+produces%22%29+AND+status+%3D+Open

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐