-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
Tyler Vallillee opened SPR-7213 and commented
I would like to know whether there is a way to specify the order of message converters on a per-controller basis.
For example, let's assume I have the following POJO:
@XmlRootElement("foo")
public class Foo {
private String messageText;
private Integer messageId;
... //getters and setters skipped
}
And I have the following controllers. First, a controller whose clients expect XML to be returned:
@Controller
public MyXmlController {
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public @ResponseBody Foo getFooById(@PathVariable int id) {
Foo foo = myService.getFooById(id);
return foo;
}
}
and now a controller whose clients expect JSON back:
@Controller
public MyJsonController {
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public @ResponseBody Foo getFooForUser(@PathVariable int id) {
Foo foo = myService.getFooByUser(id);
return foo;
}
}
Now in my servlet.xml file, I have the following to handle @ResponseBody
marshalling:
<bean id="messageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list>
<!-- Support XML marshaling -->
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="xmlMarshaller"/>
<property name="unmarshaller" ref="xmlMarshaller"/>
</bean>
<!-- Support JSON -->
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</util:list>
</property>
</bean>
The limitation here is that, given a HTTP request with an "Accept: *" header (as is typical from most web browsers) the data from MyJsonController will always be returned as XML as the XML marshaller is the first listed messageConverter for the AnnotationMethodHandlerAdapter.
Is there a way I can specify a different ordered messageConverter list that the JsonController could reference?
I am aware of ContentNegotiatingViewResolver, however in my case the API paths are legacy and difficult to update.
It would be ideal if my controller could give a hint to the MessageConverter it prefers, or if it could have its own preferred list of MessageConverters
Affects: 3.0.2
Reference URL: http://forum.springsource.org/showthread.php?p=299622