星空网 > 软件开发 > Java

SpringMvc返回@ResponseBody中文乱码

使用SpringMvc的@ResponseBody返回指定数据的类型做为http体向外输出,在浏览器里返回的内容里有中文,会出现乱码,项目的编码、tomcat编码等都已设置成utf-8,如下返回的是一个字符串中文乱码。

Java代码  SpringMvc返回@ResponseBody中文乱码

  1. @RequestMapping("user/get_comment_list.do")  
  2.     public @ResponseBody String getUserCommentList(Integer user_id,Byte type){  
  3.         HashMap<String, Object> map = new HashMap<String, Object>();  
  4.         map.put("type", type);  
  5.         map.put("user_id", user_id);  
  6.         CommentActPojo actPojo = new CommentActPojo();  
  7.         List<CommentInfo> list = this.bo.getComList(map);  
  8.         actPojo.setComments(list);  
  9.         //System.out.println("数据:"+JsonUtil.toJson(actPojo));//打印数据无中文乱码  
  10.         return JsonUtil.toJson(actPojo);  
  11.     }  



SpringMvc使用的版本是3.2.2,后来网上找了一些资料,在@RequestMapping里强制指定返回的数据类型和字符编码,中文乱码解决,如下: 

Java代码  SpringMvc返回@ResponseBody中文乱码

  1. @RequestMapping(value="user/get_comment_list.do",produces = "application/json; charset=utf-8")  


  下载地址 最主流的Java后台 SSM 框架 springmvc spring mybatis 项目源码
问题来了,如果项目里有很多类似这样的请求,每个请求都去配置produces,会很累赘且繁琐,查看了一下源代码,发现在spring处理ResponseBody时涉及到org.springframework.http.converter.StringHttpMessageConverter这个类,该类在默认实现中将defaultCharset设为ISO-8859-1。当@RequestMapping标记的方法未配置produces属性时,将自动使用默认编码;如果配置了produces属性,AbstractHttpMessageConverter中的write方法将不会受supportedMediaTypes影响,而用produce设置的header赋值给contenttype。改造一下RequestMappingHandlerAdapter的配置,springMvc.Java代码  SpringMvc返回@ResponseBody中文乱码

  1. <?"1.0" encoding="UTF-8"?>  
  2. <beans "http://www.springframework.org/schema/beans"  
  3.     "http://www.w3.org/2001/"http://www.springframework.org/schema/context"  
  4.     "http://www.springframework.org/schema/mvc"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  7.                         http://www.springframework.org/schema/context   
  8.                         http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  9.                         http://www.springframework.org/schema/mvc  
  10.                         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
  11.       
  12.     <!-- 必须放在<mvc:annotation-driven>之前 -->    
  13.     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">    
  14.         <property name="messageConverters">    
  15.             <list>    
  16.                 <bean class="org.springframework.http.converter.StringHttpMessageConverter">    
  17.                     <property name="supportedMediaTypes">    
  18.                         <list>    
  19.                             <value>text/plain;charset=UTF-8</value>  
  20.                             <value>text/html;charset=UTF-8</value>  
  21.                             <value>applicaiton/javascript;charset=UTF-8</value>    
  22.                         </list>    
  23.                     </property>    
  24.                 </bean>    
  25.             </list>    
  26.         </property>    
  27.     </bean>  
  28.       
  29.     <!-- 扫描工程文件 -->  
  30.     <context:component-scan base-package="com.tcl.club.core" />  
  31.     <context:component-scan base-package="com.cus.back" >  
  32.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />  
  33.     </context:component-scan>  
  34.       
  35.     <mvc:annotation-driven />  
  36.   
  37.     <bean id="multipartResolver"  
  38.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />  
  39.       
  40.       
  41.     <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀,在requestmapping输入的地址后自动调用该类进行视图解析 -->  
  42.     <bean id="viewResolver"  
  43.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  44.         <property name="viewClass"  
  45.             value="org.springframework.web.servlet.view.JstlView" />  
  46.         <property name="prefix" value="/WEB-INF/view/" />  
  47.         <property name="suffix" value=".jsp" />  
  48.     </bean>  
  49.       
  50. </beans>   



上述springMvc.Java代码  SpringMvc返回@ResponseBody中文乱码

  1. <bean    
  2.        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">    
  3.        <property name="supportedMediaTypes">    
  4.            <list>    
  5.                <value>application/json; charset=UTF-8</value>    
  6.                <value>application/x-www-form-urlencoded; charset=UTF-8</value>    
  7.            </list>    
  8.        </property>    
  9. </bean>   

 




原标题:SpringMvc返回@ResponseBody中文乱码

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

广东发马来西亚物流:https://www.goluckyvip.com/tag/91754.html
厦门发马来西亚物流:https://www.goluckyvip.com/tag/91755.html
上海至马来西亚物流:https://www.goluckyvip.com/tag/91757.html
杭州发马来西亚物流:https://www.goluckyvip.com/tag/91758.html
马来西亚寄中国物流:https://www.goluckyvip.com/tag/91759.html
中国寄马来西亚物流:https://www.goluckyvip.com/tag/91760.html
大福地快捷酒店预订 大福酒店怎么走:https://www.vstour.cn/a/365187.html
三亚有哪些酒店值得入住?:https://www.vstour.cn/a/366173.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流