Developer tips

Spring MVC allows you to serialize objects with @ResponseBody to any format – it has built-in converters for JSON and XML. However, even though your application may support both, you may need a default in case the Accept header is not set. Furthermore (this is a strange case, but we had it), you may want different defaults for different URLs.

As far as I know you can’t choose which of the converters to be used – they are ordered and the first converter in the list gets picked up and serializes the response.

My solution to the problem is trivial, but not the first thing you’d think about. Use a Filter, and in case the client hasn’t supplied an Accept header, wrap the request and make it return the default Accept header, as if the client has set it. That way, whenever spring-mvc invokes the getHeader() method, it will get the desired Accept header.

(btw, please let me know if there’s a native spring way to do that)

There are Spring MVC native ways. On an individual method you can use the “produces” attribute of @RequestMapping. It influences the mapping of the request and also the resulting content type of the response. In the absence of an Accept header or in case of ‘Accept=*/*’, the specified producible media type is used.

In Spring MVC 3.2 (currently RC1) you can also configure a ContentNegotiationStrategy to decide the acceptable media type. There are several built-in strategies and they can be ordered through ContentNeogitationManager. See the what’s new in Spring 3.2 section and this presentation.

Interesting, from the documentation and attempts of a colleague of mine it turned out “produces” limits the type to the specified ones. What happens if you have produces={“application/json”, “application/xml”} ?

As far as I understood, spring 3.1 rejects anything that is not Accept=application/json or application/xml.
And if it doesn’t, does it take into account the order of declaration? Or just goes through the list of converters and picks the first one?

(spring 3.2 is, alas, not an option, the project is in production and upgrade to RC1 is not feasible)

Hi! This is my first visit to your blog! We are a team of volunteers
and starting a new project in a community in the same niche.
Your blog provided us valuable information to work on.
You have done a extraordinary job!