网站建设资讯

NEWS

网站建设资讯

SpringMvc返回@ResponseBody出现中文乱码该怎么办-创新互联

SpringMvc返回@ResponseBody出现中文乱码该怎么办,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联公司专注于肃北网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供肃北营销型网站建设,肃北网站制作、肃北网页设计、肃北网站官网定制、小程序制作服务,打造肃北网络公司原创品牌,更为您提供肃北网站排名全网营销落地服务。

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

Java代码

  1. @RequestMapping("user/get_comment_list.do")

  2.     public @ResponseBody String getUserCommentList(Integer user_id,Byte type){

  3.         HashMap map = new HashMap();

  4.         map.put("type", type);

  5.         map.put("user_id", user_id);

  6.         CommentActPojo actPojo = new CommentActPojo();

  7.         List 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代码

  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.xml如下:

Java代码

  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

  2.     xmlns:mvc="http://www.springframework.org/schema/mvc"

  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans

  4.                         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

  5.                         http://www.springframework.org/schema/context

  6.                         http://www.springframework.org/schema/context/spring-context-3.2.xsd

  7.                         http://www.springframework.org/schema/mvc

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

  9.     

  10.     

  11.         

  12.             

  13.                 

  14.                     

  15.                         

  16.                             text/plain;charset=UTF-8

  17.                             text/html;charset=UTF-8

  18.                             applicaiton/javascript;charset=UTF-8

  19.                         

  20.                     

  21.                 

  22.             

  23.         

  24.     

  25.     

  26.     

  27.     

  28.         

  29.     

  30.     

  31.     

  32.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

  33.     

  34.     

  35.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  36.         

  37.             value="org.springframework.web.servlet.view.JstlView" />

  38.         

  39.         

  40.     



上述springMvc.xml文件重新设置了StringHttpMessageConverter的编码方式,而不必在每个@RequestMapping里设置produces属性。如果返回的Jackson类型的数据,可以设置成如下:

Java代码

  1.        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

  2.        

  3.            

  4.                application/json; charset=UTF-8

  5.                application/x-www-form-urlencoded; charset=UTF-8

  6.            

  7.        

看完上述内容,你们掌握SpringMvc返回@ResponseBody出现中文乱码该怎么办的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享标题:SpringMvc返回@ResponseBody出现中文乱码该怎么办-创新互联
标题链接:http://cdweb.net/article/dchihi.html