6
votes

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...

1
Do you have a HTTPMessageConverter to have the produces type? When you crank up logging what do you get? - chrislovecnm
Yes, I do. If you're curious: you have to add application/*+json to the supported media types of the Jackson message converter. If I delete one Method, it's working. - Felix
I'm only getting the error mentioned above. - Felix
Could it be that Spring is checking the produces values against the MediaType class? Maybe you need to extend that class and add your custom values to that? - nickdos
I don't think so, because it doesn't work either if I change the media types of my example to application/json and application/xml. - Felix

1 Answers

1
votes

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