简体   繁体   中英

How to change content type of MappingJacksonHttpMessageConverter from application/json;charset=UTF-8 to application/json

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>

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-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM