SpringMVC使用注解@ResponseBody返回json中文乱码

  ---------------------------------------

   在这里我不得支持一下SpringMVC是一个不错的框架,用了springmvc这么长时间  我犯了个错误,说来惭愧.

在项目中编码格式是UTF-8    在使用Ajax 请求  在SpringMVC中返回Json 数据 我用到了@ResponseBody 注解  将表示该方法的返回结果直接写入HTTP response body中。 

 

  然后再数据库中取出的中文通过Spring的response返回结果中文乱码.

 

方法一

原因:

response在响应的时候 生成的response中"Content-Type"的值不正确。(默认的是ISO-8859-1)


然后使用Spring使用AnnotationMethodHandlerAdapter来处理@ResponseBody,该类再使用一些HttpMessageConverter来具体处理信息。 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.     http://www.springframework.org/schema/context  
  9.     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.     http://www.springframework.org/schema/aop  
  11.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  12.     http://www.springframework.org/schema/tx  
  13.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  14.     http://www.springframework.org/schema/mvc  
  15.     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  16.       
  17.     <bean    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >  
  18.             <property name="messageConverters">     
  19.          <list>     
  20.              <bean class = "org.springframework.http.converter.StringHttpMessageConverter">     
  21.                 <property name = "supportedMediaTypes">  
  22.                       <list>  
  23.                           <value>text/html;charset=UTF-8</value>     
  24.                      </list>     
  25.                 </property>     
  26.              </bean>     
  27.          </list>     
  28.    </property>    
  29.     </bean>  
  30.     <context:component-scan base-package="com.zdkj.limp.controller"></context:component-scan>  
  31.     <mvc:annotation-driven />  
  32.     <bean  
  33.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  34.         <property name="prefix" value="/" />  
  35.         <property name="suffix" value=".jsp"></property>  
  36.         <property name="viewClass"  
  37.             value="org.springframework.web.servlet.view.JstlView" />  
  38.     </bean>  
  39.       
  40. </beans>  

 
 

以上是我就纠正回来的正确代码   以免误导大家  我贴出了正确的代码

 

 

下面那段是错误的代码

 

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.     http://www.springframework.org/schema/context  
  9.     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.     http://www.springframework.org/schema/aop  
  11.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  12.     http://www.springframework.org/schema/tx  
  13.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  14.     http://www.springframework.org/schema/mvc  
  15.     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  16.   
  17.     <context:component-scan base-package="com.zdkj.limp.controller"></context:component-scan>  
  18.     <mvc:annotation-driven />  
  19.     <bean  
  20.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  21.         <property name="prefix" value="/" />  
  22.         <property name="suffix" value=".jsp"></property>  
  23.         <property name="viewClass"  
  24.             value="org.springframework.web.servlet.view.JstlView" />  
  25.     </bean>  
  26.     <bean    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >  
  27.             <property name="messageConverters">     
  28.          <list>     
  29.              <bean class = "org.springframework.http.converter.StringHttpMessageConverter">     
  30.                 <property name = "supportedMediaTypes">  
  31.                       <list>  
  32.                           <value>text/html;charset=UTF-8</value>     
  33.                      </list>     
  34.                 </property>     
  35.              </bean>     
  36.          </list>     
  37.    </property>    
  38.     </bean>  
  39. </beans>  

 

 

大家看看有什么区别   

我找了半天都没找出原因  后来发现顺序不对

程序执行的时候先注解扫描包  没发现  org.springframework.http.converter.StringHttpMessageConverter

 

最后才执行org.springframework.http.converter.StringHttpMessageConverter当执行的时就不再去扫描了

所以最终结果是顺序放错了

 

 

 

 

----------------------------------------------------------------------------

 

 

附加的一个案例如果大家再碰到这种乱码问题   没办法  使用以下代码

 

方法 二

使用java中的urlEncoding 对字符进行编码  然后再到客户端js中进行urlDecoding进行解码  也可以解决乱码问题  但我不建议使用这种方法  治标不治本

 

 

服务器端   java代码如下:

Java代码   收藏代码
  1. // URL编码  
  2. protected String urlEncode(String src) {  
  3.     try {  
  4.         return URLEncoder.encode(src, "UTF-8");  
  5.     } catch (Exception ex) {  
  6.         return src;  
  7.     }  
  8. }  
 

 

 

客户端 js中 代码如下:

 

<script type="text/javascript">

Js代码   收藏代码
  1.     if (!misc) {  
  2.         var misc = {};  
  3.     }  
  4.     if (!misc.utils) {  
  5.         misc.utils = {};  
  6.     }  
  7.     misc.utils.urlencode2Utf8 = function(ptr) {  
  8.         for ( var x in ptr) {  
  9.             if (typeof (ptr[x]) == 'object') {  
  10.                 misc.utils.urlencode2Utf8(ptr[x]);  
  11.             } else if (typeof (ptr[x]) == 'string') {  
  12.                 try {  
  13.                     ptr[x] = decodeURIComponent(ptr[x]);  
  14.                 } catch (e) {  
  15.   
  16.                 }  
  17.             }  
  18.         }  
  19.     };  
  20. </script>  
 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值