使用SpringMvc的@ResponseBody返回指定数据的类型做为http体向外输出,在浏览器里返回的内容里有中文,会出现乱码,项目的编码、tomcat编码等都已设置成utf-8,如下返回的是一个字符串中文乱码。
@RequestMapping("user/get_comment_list.do")
public @ResponseBody String getUserCommentList(Integer user_id,Byte type){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("type", type);
map.put("user_id", user_id);
CommentActPojo actPojo = new CommentActPojo();
List<CommentInfo> list = this.bo.getComList(map);
actPojo.setComments(list);
//System.out.println("数据:"+JsonUtil.toJson(actPojo));//打印数据无中文乱码
return JsonUtil.toJson(actPojo);
}
SpringMvc使用的版本是3.2.2,后来网上找了一些资料,在@RequestMapping里强制指定返回的数据类型和字符编码,中文乱码解决,如下:
@RequestMapping(value="user/get_comment_list.do",produces = "application/json; charset=utf-8")
问题来了,如果项目里有很多类似这样的请求,每个请求都去配置produces,会很累赘且繁琐,查看了一下源代码,发现在spring处理ResponseBody时涉及到org.springframework.http.converter.StringHttpMessageConverter这个类,该类在默认实现中将defaultCharset设为ISO-8859-1。当@RequestMapping标记的方法未配置produces属性时,将自动使用默认编码;如果配置了produces属性,AbstractHttpMessageConverter中的write方法将不会受supportedMediaTypes影响,而用produce设置的header赋值给contenttype。改造一下RequestMappingHandlerAdapter的配置,springMvc.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">
<!-- 必须放在<mvc:annotation-driven>之前 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
<value>applicaiton/javascript;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<!-- 扫描工程文件 -->
<context:component-scan base-package="com.tcl.club.core" />
<context:component-scan base-package="com.cus.back" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<mvc:annotation-driven />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀,在requestmapping输入的地址后自动调用该类进行视图解析 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
上述springMvc.xml文件重新设置了StringHttpMessageConverter的编码方式,而不必在每个@RequestMapping里设置produces属性。如果返回的Jackson类型的数据,可以设置成如下:
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json; charset=UTF-8</value>
<value>application/x-www-form-urlencoded; charset=UTF-8</value>
</list>
</property>
</bean>
分享到:
相关推荐
SpringMVC使用@ResponseBody.rar
--处理 @ResponseBody 中文乱码问题 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!-- Support...
Spring MVC下彻底解决@ResponseBody转义html特殊字符问题,解决包括尖角号(),单引号('),双引号("),按位与(&),反斜杠(\)。
主要介绍了SpringMVC中解决@ResponseBody注解返回中文乱码问题, 小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Springmvc 4.x利用@ResponseBody返回Json数据的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
springMVC返回json数据需要的两个架包,加入springMVC使用@ResponseBody才不会报错。
springmvc关于@requestMapping和@requestParam的使用,详细代码,独立可用无需修改。
springmvc之@RequestMapping的简单demo
NULL 博文链接:https://bijian1013.iteye.com/blog/2310240
springmvc+国际化i18N+springmvc验证+jetbrick-template使用+@responsebody+谷歌guava: 1)围绕springmvc做的国际化 2)围绕springmvc做的验证 3)使用的jetbrick-template模板引擎 ……
本篇文章主要介绍了springMvc注解之@ResponseBody和@RequestBody详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
如果传输的是多层嵌套json对象,这个时候会就会出现数据丢失问题 @ResponseBody很好的解决了这个问题,它会把前台传输过来的json转化为后台对应的对象
本篇文章主要介绍了详解SpringMVC注解@initbinder解决类型转换问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
1)spring MVC 中@ResponseBody需要的所有JAR包 2)性能还不错的模板引擎jetbrick-template-2.x 2.x的所需的所有jar包 3)日志jar包:slf4j和logback 所有 4)阿里 druid 连接池jar包 5)mysql数据库链接驱动jar包 6...
NULL 博文链接:https://bijian1013.iteye.com/blog/2310236
springmvc进行Jason数据封装转化时可以即使用也是@responsebody对数据进行封装的时候需要的工具类
本篇文章主要介绍了SpringMVC使用@Valid注解进行数据验证的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了使用SpringMVC的@Validated注解验证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
springMVC框架遇到中文乱码的解决思路汇总,包括从配置到注解的详细描述,帮助开发人员解决spirngMVC中文乱码问题
SpringMVC @ModelAttribute应用SpringMVC @ModelAttribute应用