Reputation: 548
I have a spring REST web service and in my controllers I am using MappingJacksonHttpMessageConverter
to convert my returning models into JSON. But when I check it with firebug
there is Content-Type=application/json;charset=UTF-8
.
Also I am trying to parse this result from an android client by using spring android rest template but I keep getting:
Could not extract response: no suitable HttpMessageConverter found for response type [xxx.SamplePageActivity$Result] and content type application/json;charset=UTF-8]
It maybe the case that MappingJacksonHttpMessageConverter on the android client side expects exactly the type application/json
So my question is how to change spring's MappingJacksonHttpMessageConverter's returning Content-Type from application/json;charset=UTF-8
to application/json
.
Here is my view resolver config. It is maybe useful :
<beans:bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<beans:property name="mediaTypes">
<beans:map>
<beans:entry key="html" value="text/html" />
<beans:entry key="json" value="application/json" />
</beans:map>
</beans:property>
<beans:property name="viewResolvers">
<beans:list>
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value="${dispatcher.suffix}" />
</beans:bean>
</beans:list>
</beans:property>
<beans:property name="defaultViews">
<beans:list>
<beans:bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="messageAdapter"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<!-- Support JSON -->
<beans:bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>
Upvotes: 4
Views: 4015
Reputation: 12575
you can more precisely configure the MappingJacksonHttpMessageConverter using the supportedMediaTypes property, like so:
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg value="application" />
<constructor-arg value="json" />
<constructor-arg value="#{T(java.nio.charset.Charset).forName('UTF-8')}"/>
</bean>
</list>
</property>
</bean>
which, according to docs ( http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/http/MediaType.html) lets you set the type, subtype and charset, in that order-
Upvotes: 3