zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Educational Codeforces Round 80 (Rated for Div. 2)
    2020 CCPC Wannafly Winter Camp
    Codeforces Round #613 (Div. 2)
    Codeforces Round #612 (Div. 2)
    Hello 2020
    Good Bye 2019
    Codeforces Round #590 (Div. 3)
    依赖注入
    Spring 拦截器
    rsync服务端一键安装rsync脚本(非源码)
  • 原文地址:https://www.cnblogs.com/Cilimer/p/4530396.html
Copyright © 2011-2022 走看看