I want to serve different content types based on the Accept header I receive at a RESTful endpoint.
application/myapp-v1.0+json
application/myapp-v1.0+xml
Is there any way to do this with @ResponseBody? It seems like a convenient shortcut by allowing me to just return an object and let the HttpMessageConverter handle invoking Jackson but I just cannot get it working. 406 responses any time I wire up more than one MimeType.
For this iteration I’ve ended up querying the request header myself and returning the appropriate view handler myself, but I wouldn’t mind revisiting this in future.
If you are working with Spring 3.1, you can do this using
@RequestMapping. New to 3.1 for theRequestMappingannotation are these members:consumes()— Allows you to filter by theContent-typerequest header.produces()— Allows you to filter by theAcceptrequest header.There is also the ability to apply some simple expressions to make it more robust.