Spring MVC 3.1 - 版本化REST Web服务

Fel*_*lix 6 versioning rest spring spring-mvc

我想通过为不同版本提供不同的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();
}
}
Run Code Online (Sandbox Code Playgroud)

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

java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path '/test'
Run Code Online (Sandbox Code Playgroud)

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

sdo*_*ass 1

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-products

按照你的方式,这应该是可能的。如何在 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+products%22+OR+description+%7E+%22mvc+products%22% 29+AND+状态+%3D+打开