@ResponseBody中文乱码解决方案

java web项目,使用了springmvc4.0,用@ResponseBody返回中文字符串,乱码$$
本以为很简单的问题,不过也找了一个小时。
网上有说这样配置的:

<mvc:annotation-driven>
    <mvc:message-converters>
	<bean class="org.springframework.http.converter.StringHttpMessageConverter">
                 <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

兴奋的试了,nnd,不行
接着找,有说这样配的:

<mvc:annotation-driven>
	<mvc:message-converters>
		<bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                     <property name = "supportedMediaTypes">
                        <list>
                            <value>text/plain;charset=UTF-8</value>   
                        </list> 
                     </property>
                </bean>
	</mvc:message-converters>
</mvc:annotation-driven>

注意
如果返回的字符串中有html标签,那么list标签里再加上

<value>text/html;charset=UTF-8</value>

,不然还是乱码!
再试试,行了哎!!
为啥这样配置就行,有时间在看看源码吧。

原文地址:https://www.cnblogs.com/westward/p/6930182.html