我已将我的休息服务的异常处理集中到一个简洁的ControllerAdvice中。
我正在返回常规的传输对象,希望我的酷映射 - jackson-converter将它转换为最终客户端的JSON。
现在就是这样。如果我没有将accept-header设置为“Application / JSON”,我没有得到转换后的JSON,我得到了一些默认的HTML,在我看似由Jetty生成的测试中。我不得不承认我不知道为什么,但我想这是一些默认的Spring解析器生效。
这让我思考。调用我的休息URL的客户应该知道我返回JSON,所以我希望我的服务总是返回json。
有没有办法配置ReqeustMappingHandlerAdapter以始终生成JSON?
我当前的配置:
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<!-- Instantiation of the converter in order to configure it -->
<beans:bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<!--beans:property name="supportedMediaTypes" value="application/json"/-->
</beans:bean>
答案 0 :(得分:1)
我不知道这是否能解答您的问题,但您可以将Spring配置中的默认内容类型设置为JSON(默认为没有Spring MVC中的Accept标头的HTML)。以下是一个示例配置:
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="defaultContentType" value="application/json" />
</bean>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
来源:
http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc