Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC 3.1 - versioning REST webservices

I'd like to version my REST-Webservice by providing different Accept header values for different versions (see http://barelyenough.org/blog/2008/05/versioning-rest-web-services/).

The problem is, it does not seem to be possible with Spring MVC 3.

My controller looks like this:

@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();
}
}

When I try to access one of these methods, I get an Exception:

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

Am I missing something, or ist this impossible with Spring MVC? I know that it is possible with JAX-RS...

like image 509
Felix Avatar asked Mar 12 '12 18:03

Felix


1 Answers

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

This should be possible the way you have things. How are you specifying the Accept header in the GET request? Are you 100% positive your GET request is sending an Accept header value that will only match one or the other of the content types you've specified? If you send a header that matches both then Spring will not know which handler method should handle the request.

You may need to turn up org.springframework logging to DEBUG to see what is going on, or use a breakpoint debugger and the Spring source code to see what is really happening. "produces" is a relatively new feature so it's also possible there is a bug.

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

like image 173
sdouglass Avatar answered Oct 22 '22 08:10

sdouglass