pringMVC 配置StringHttpMessageConverter不生效问题

当使用@ResponseBody返回的字符串带有中文时,返回类型为String会被StringHttpMessageConverter处理,当时查看源码发现默认的Charset DEFAULT_CHARSET使用的是ISO-8859-1。
context.xml里有配置如下信息时候,但是发现没有生效。需要把这段配置在<mvc:annotation-driven/>注解前面!
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" value="#{jsonUtil.objectMapper}"/>
<property name="supportedMediaTypes">
<list>
<value>text/json;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>

</bean>
<mvc:annotation-driven/> //这行配置在后面上面的配置才会生效,spring启动的时候这行注解做的事情会初始化各种配置,包括上面的配置所做的事情,后面再注入到spring是不生效的!
原文地址:https://www.cnblogs.com/shown/p/6518669.html