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>
  • 相关阅读:
    git简单使用
    简单Spring和mybatis整合配置文件
    ASP.NET程序开发范例宝典
    C# DataSet和DataTable详解
    AOP
    匿名内部类
    数据库事务
    mybatis
    线程池
    单例模式
  • 原文地址:https://www.cnblogs.com/Cilimer/p/4530396.html
Copyright © 2011-2022 走看看