Search code examples
jsonrestxstreamspring-3

Spring 3 with json and xstream output not working


I have this working now now, but am lost as to why this problem occurred..

I followed the following

http://pfelitti87.blogspot.co.uk/2012/07/rest-services-with-spring-3-xml-json.html

but i changed the controller method and added @ResponseBody...

@ResponseBody
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value="/names", method=RequestMethod.GET)
public List<Book> getNames() {

  return returnData();
}

By adding this i noticed that the output would appear as json, regardless of the extension i specified?...

Any ideas why @RepsonseBody would cause this issue?


Solution

  • The post only works for resolving different views based on different types. It does not work on your case.

    If you are using Spring 3.2.x, the configuration below would solve your problem.

     <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>
    
      <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="favorPathExtension" value="true"/>
        <property name="mediaTypes">
          <value>
            json=application/json
            xml=application/xml
          </value>
        </property>
        <property name="defaultContentType" value="application/json"/>
      </bean>
    

    However, if you are using 3.1.x, there are approaches like http://tedyoung.me/2011/07/28/spring-mvc-responsebody and http://springinpractice.com/2012/02/22/supporting-xml-and-json-web-service-endpoints-in-spring-3-1-using-responsebody that might help you.