SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.

SpringMVC的@ResponseBody,返回json,如果有中文显示乱码的解决办法.

在SpringMVC的配置文件中

<bean  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    </bean>

查看了AnnotationMethodHandlerAdapter的源码,发现其默认编码为构造参数有StringHttpMessageConverter对象。

继续深入查看,发现StringHttpMessageConverter的默认编码竟然是“ISO-8859-1”,难怪在用utf-8显示的时候会显示乱码。

下面是解决方案:

在AnnotationMethodHandlerAdapter中,自定义StringHttpMessageConverter的编码格式为UTF-8即可。

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class = "org.springframework.http.converter.StringHttpMessageConverter">     
                <property name = "supportedMediaTypes">  
                      <list>  
                          <value>text/html;charset=UTF-8</value>     
                     </list>     
                </property>     
             </bean>     
            </list>
        </property>
    </bean>
原文地址:https://www.cnblogs.com/Cilimer/p/4530396.html