Announcement Announcement Module
No announcement yet.
RestTemplate, @ResponseBody,ContentNegotiatingViewResolver and AnnotationMethodHandle Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • RestTemplate, @ResponseBody,ContentNegotiatingViewResolver and AnnotationMethodHandle


    I have had a bit of a struggle getting a Rest Template that uses JSON and XML to talk to a RestController that should return either XML or JSON.

    These seem to be the issues:

    1. @ResponseBody and ContentNegotiatingViewResolver do not coexist

    The problem is that if I use @ResponseBody then the JSON serialisation works correctly but @ResponseBody
    causes ContentNegotiatingViewResolver to be ignored and I always get JSON and never XML returned.

    2. ContentNegotiatingViewResolver with MappingJacksonJsonView returns wrong JSON for RestTemplate

    If I remove the @ResponseBody then the ContentNegotiatingViewResolver does kick in and the XML or JSON
    view is correctly interpreted (I am using urls ending .xml or.json and setting favorPathExtension to true).
    The problem with this is that MappingJacksonJsonView does not return JSON that MappingJacksonHttpMessageConverter
    used by the RestTemplate can handle.

    This blog post touches on the issue: but
    I get the feeling that this example does not use the RestTemplate to process the results.

    <bean class="org.springframework.web.servlet.view.Conten tNegotiatingViewResolver">
    <property name="mediaTypes">
    <entry key="xml" value="application/xml"/>
    <entry key="json" value="application/json"/>

    <property name="defaultViews">
    <bean class="org.springframework.web.servlet.view.xml.Ma rshallingView">
    <constructor-arg ref="jibxMarshaller"/>
    <bean class="org.springframework.web.servlet.view.json.M appingJacksonJsonView"/>
    <property name="ignoreAcceptHeader" value="true" />
    <property name="favorPathExtension" value="true" />
    <property name="favorParameter" value="false" />

    3. message-converters tag in mvc:annotation-driven appears to be ignored.

    Here is my annotation-driven configuration which will return JSON and not XML:

    <mvc:message-converters register-defaults="false">
    <bean class="org.springframework.http.converter.json.Map pingJacksonHttpMessageConverter" />
    <bean class="org.springframework.http.converter.xml.Mars hallingHttpMessageConverter">
    <property name="marshaller" ref="jibxMarshaller" />
    <property name="unmarshaller" ref="jibxMarshaller" />

    Running through with the debugger indicates that register-defaults is being ignored and the jibx marshaller is not being picked up - if I used JAXB it would probably work.

    4. AnnotationMethodHandlerAdapter instead of mvc-annotation driven

    Things work if you use AnnotationMethodHandlerAdapter instead of mvc-annotation-driven tag.

    <bean class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping">

    <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
    <bean class="org.springframework.http.converter.json.Map pingJacksonHttpMessageConverter" >
    <property name="supportedMediaTypes" value="application/json" />
    <bean class="org.springframework.http.converter.xml.Mars hallingHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/xml"/>
    <property name="marshaller" ref="jibxMarshaller" />
    <property name="unmarshaller" ref="jibxMarshaller" />

    5. RestTemplate and Accept types

    AnnotationMethodHandlerAdapter expects the Http Header to have the correct Accept types.

    6. What would be nice

    To get things working I have used AnnotationMethodHandlerAdapter with RestTemplate setting Accept types.

    It would be nice if either (1) mvc:annotation-driven handled the message-converters correctly or (2) mvc:annotation-driven and ContentNegotiatingViewResolver could co-exist so that you can interpret xml or json on path extensions not just on Accept headers or (3) ContentNegotiatingViewResolver could write JSON that the RestTemplate could handle.

    I was wondering whether other people had made this journey or maybe there is a mistake in some of this.