Android – how to change the content type of applicationjacksonhttpmessageconverter from application / JSON; Charset = UTF-8 changed to appl

I have a @ L_ 301_ 0 @ rest web service. In my controller, I use mappingjacksonhttpmessageconverter to convert my return model into JSON. However, when I check it with firebug, there is content type = Application / JSON; charset = UTF-8.

In addition, I tried to parse this result from the Android client by using the spring Android rest template, but I kept getting:

It may be that the mappingjacksonhttpmessageconverter on the Android client completely expects the type application / JSON

So my question is how to change the spring mappingjacksonhttpmessageconverter to return content type from application / JSON; Charset = UTF-8 to application / JSON

This is my view parser configuration. This may be 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>

resolvent:

You can use the supportedmediatypes property to configure the mappingjacksonhttpmessageconverter more precisely, as follows:

<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>

According to document( http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/http/MediaType.html ), you can set types, subtypes, and character sets in order –

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>