使用SpringMvc的@ResponseBody返回指定数据的类型做为http体向外输出,在浏览器里返回的内容里有中文,会出现乱码,项目的编码、tomcat编码等都已设置成utf-8,如下返回的是一个字符串中文乱码。Java代码 @RequestMapping(&qu ...
使用SpringMvc的@ResponseBody返回指定数据的类型做为http体向外输出,在浏览器里返回的内容里有中文,会出现乱码,项目的编码、tomcat编码等都已设置成utf-8,如下返回的是一个字符串中文乱码。
Java代码
- @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里强制指定返回的数据类型和字符编码,中文乱码解决,如下: Java代码
- @RequestMapping(value="user/get_comment_list.do",produces = "application/json; charset=utf-8")
上述springMvc.Java代码
- <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中文乱码
关键词:Spring
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。