【发布时间】:2015-10-05 20:30:36
【问题描述】:
我们目前有一个响应 json 的 Spring App。我们也在尝试支持 xml 响应。目前,控制器方法看起来像
@RequestMapping(value = URIConstants.POSTS, method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public @ResponseBody RestResponse<PostResponse> getItems(@RequestBody Request request, HttpServletRequest httpRequest) throws Exception {
// do stuff
}
我们在服务器配置 xml 中添加了以下内容:
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true"/>
<property name="ignoreAcceptHeader" value="true" />
<property name="useJaf" value="false"/>
<property name="defaultContentType" value="application/json" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
这是完整的配置文件:
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true"/>
<property name="ignoreAcceptHeader" value="true" />
<property name="useJaf" value="false"/>
<property name="defaultContentType" value="application/json" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
<bean id="spurJSONMapper" class="com.ct.app.utilities.SpurJSONMapper"/>
<mvc:interceptors>
<ref bean="interceptor"/>
</mvc:interceptors>
<bean id="interceptor"
class="com.ct.app.controller.RequestInterceptor">
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMapping HandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
当我们请求资源时,我们得到以下响应:“服务器拒绝此请求,因为请求实体的格式不受所请求方法的请求资源支持。”
不确定配置中的漏洞在哪里,感谢任何帮助。
【问题讨论】:
标签: java xml spring spring-mvc