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

  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里强制指定返回的数据类型和字符编码,中文乱码解决,如下:

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

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

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

  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中文乱码的更多相关文章

  1. 解决springmvc返回json中文乱码

    在pringmvc中通过设置@ResponseBody返回json乱码问题,这个问题上网找了很久,发现答案真是人云亦云,奉上我的解决方案: 解决方案一:需要导入 jackson-core-asl-1. ...

  2. SpringMvc Controller请求链接忽略大小写(包含拦截器)及@ResponseBody返回String中文乱码处理

    SpringMvc Controller请求链接忽略大小写(包含拦截器)及@ResponseBody返回String中文乱码处理... @RequestMapping(value = "/t ...

  3. 解决springmvc使用ResponseBody注解返回json中文乱码问题

    spring版本:4.2.5.RELEASE 查看“org.springframework.http.converter.StringHttpMessageConverter”源码,中有一段说明: B ...

  4. 解决springmvc+mybatis+mysql中文乱码问题【转】

    这篇文章主要介绍了解决java中springmvc+mybatis+mysql中文乱码问题的相关资料,需要的朋友可以参考下 近日使用ajax请求springmvc后台查询mysql数据库,页面显示中文 ...

  5. Hession集成Spring + maven依赖通讯comm项目 + 解决@ResponseBody中文乱码

    hessian结合spring的demo         hessian的maven依赖: <!-- hessian --> <dependency>         < ...

  6. springMvc解决json中文乱码

    springMvc解决json中文乱码 springMvc解决json中文乱码,springMvc中文乱码,spring中文乱码 >>>>>>>>> ...

  7. 如何解决http请求返回结果中文乱码

    如何解决http请求返回结果中文乱码 1.问题描述 http请求中,请求的结果集中包含中文,最终以乱码展示. 2.问题的本质 乱码的本质是服务端返回的字符集编码与客户端的编码方式不一致. 场景的如服务 ...

  8. SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.

    SpringMVC的@ResponseBody,返回json,如果有中文显示乱码的解决办法. 在SpringMVC的配置文件中 <bean class="org.springframe ...

  9. SpringMVC 使用@ResponseBody返回json 中文乱码与返回实体类报错

    有时候我们发现接收的是中文,返回却是个?.这确实是个蛋疼的问题,Spring中解析字符串的转换器默认编码居然是ISO-8859-1 /** * Implementation of {@link Htt ...

随机推荐

  1. Delphi语言最好的JSON代码库 mORMot学习笔记1(无数评论)

    mORMot没有控件安装,直接添加到lib路径,工程中直接添加syncommons,syndb等到uses里 --------------------------------------------- ...

  2. elasticsearch索引查询,日志搜素

    索引查询 http://10.199.137.115:9200/_cat/indices?format=json 返回json字符串的索引状态 增加索引名称过滤 http://10.199.137.1 ...

  3. YTU 2481: 01字串

    2481: 01字串 时间限制: 1 Sec  内存限制: 128 MB 提交: 103  解决: 72 题目描述 对于长度为7位的一个01串,每一位都可能是0或1,一共有128种可能.它们的前几个是 ...

  4. hadoop异常:Be Replicated to 0 nodes, instead of 1

    Hadoop 坑爹的Be Replicated to 0 nodes, instead of 1 异常 博客分类: Java 编程 HadoopITeyeJSP算法Apache  有段时间不写博客了, ...

  5. codeforces 690C1 C1. Brain Network (easy)(水题)

    题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  6. 初学者对springMVC的认识

    首先是要一定说明的是,这倒是说明是什么?对吧 Spring MVC 是SpringFrameWork的后续产品,并且已经融入到Spring Web Flow中 同时Spring MVC 分离了控制器, ...

  7. Educational Codeforces Round 23

    A题 分析:注意两个点之间的倍数差,若为偶数则为YES,否则为NO #include "iostream" #include "cstdio" #include ...

  8. [转载]文件过滤驱动 文件系统激活通知 IoRegisterFsRegistrationChange函数实现

    IoRegisterFsRegistrationChange 注册一个文件系统变动回调函数,用来被通知文件系统的激活和注销,激活是指第一次加载文件系统,当一个文件系统已经加载后,当加载一个同种文件系统 ...

  9. C#中的Webservice实例代码(vs2013)

    2.1首先创建一个最基本的web service服务端,顾名思义就是提供服务,这儿实现一个简单的加法计算. 首先,vs2013--文件---新建项目---Asp.net 空Web 应用程序    (V ...

  10. 黑客攻防技术宝典web实战篇:攻击用户·其他技巧习题

    猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 已知一项应用程序功能将一个查询字符串参数的内容插入到某个 HTTP 重定向的 Locati ...