SpringMVC ResponseBody返回字符串(JSON)乱码

SpringMVC的@ResponseBody注解可以将请求方法返回的对象直接转换成JSON对象,但是当返回值是String的时候,中文会乱码,原因是因为其中字符串转换和对象转换用的是两个转换器,而String的转换器中固定了转换编码为”ISO-8859-1”,网上也很多种解决方法,有通过配置Bean编码的,也有自己重写转换器。

有效的解决方法是:

在spring-mvc.xml中配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
    <context:component-scan base-package="com.baobaotao.controller" />

    <mvc:annotation-driven>
  <mvc:message-converters register-defaults="true">
    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
      <constructor-arg value="UTF-8" />
    </bean>
  </mvc:message-converters>
</mvc:annotation-driven>
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="ignoreAcceptHeader" value="false"/>
        <property name="defaultContentType" value="text/html"/>
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html;charset=UTF-8"/>
                <entry key="json" value="application/json;charset=UTF-8"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="10"/>
                <!--use jsp view resolver-->
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="100">  
                    <property name="prefix" value="/WEB-INF/views/"/>  
                    <property name="suffix" value=".jsp"/>
                    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
               </bean>
            </list>
        </property>
    </bean>

注意以上配置需要放到之前,否则无效。

看上面的配置 有时候会出现
‘mvc:annotation-driven’ must have no character or element问题,就是不能有子元素。
原因是要mvc 3.0以上,其实你不用指定,默认用最新的

 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"

如果还不行替换xml文件头部:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context-3.2.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  

针对乱码问题网上普遍的两种解决方法是:
1.1.在action中取得response,由他写入响应数据。

response.setHeader("Cache-Control", "no-cache");   
        response.setContentType("text/json;charset=UTF-8");  
        response.setCharacterEncoding("UTF-8");  
PrintWriter out = response.getWriter();  
            out.write(result);  

这样每次都要写很麻烦

2.方法一:
自己继承AbstractHttpMessageConverter,写一个类复制 StringHttpMessageConverter.java的代码,将
public static final Charset DEFAULT_CHARSET = Charset.forName(“ISO-8859-1”);
改为
public static final Charset DEFAULT_CHARSET = Charset.forName(“UTF-8”);
spring-servlet的配置文件如下

<bean  
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
    <property name="messageConverters">  
        <list>  
            <bean class="com.renren001.converter.UTF8StringHttpMessageConverter" />  
        </list>  
    </property>  
</bean>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值