我现在已经开始工作,但是为什么会出现这个问题我很遗憾。
我遵循以下
http://pfelitti87.blogspot.co.uk/2012/07/rest-services-with-spring-3-xml-json.html
但是我更改了控制器方法并添加了@ResponseBody ...
@ResponseBody
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value="/names", method=RequestMethod.GET)
public List<Book> getNames() {
return returnData();
}
通过添加这个我注意到输出将显示为json,无论我指定的扩展名是什么?...
为什么@RepsonseBody会导致这个问题?
答案 0 :(得分:2)
该帖子仅适用于根据不同类型解析不同视图。它对你的情况不起作用。
如果您使用的是Spring 3.2.x,则下面的配置可以解决您的问题。
<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>
但是,如果您使用的是3.1.x,那么http://tedyoung.me/2011/07/28/spring-mvc-responsebody和http://springinpractice.com/2012/02/22/supporting-xml-and-json-web-service-endpoints-in-spring-3-1-using-responsebody等方法可能会对您有所帮助。