解决Spring MVC @ResponseBody返回中文字符串乱码问题
来源:互联网 发布:辐射4萌妹捏脸数据 编辑:程序博客网 时间:2024/06/05 10:44
引起乱码原因为spring mvc使用的默认处理字符串编码为ISO-8859-1,具体参考org.springframework.http.converter.StringHttpMessageConverter类中public static final Charset DEFAULT_CHARSET = Charset.forName(“ISO-8859-1”);
解决方法:
第一种方法:
对于需要返回字符串的方法添加注解,如下:
@RequestMapping(value="/getUsers", produces = "application/json; charset=utf-8") public String getAllUser() throws JsonGenerationException, JsonMappingException, IOException { List<User> users = userService.getAll(); ObjectMapper om = new ObjectMapper(); System.out.println(om.writeValueAsString(users)); DataGrid dg = new DataGrid(); dg.setData(users); return om.writeValueAsString(dg); }
此方法只针对单个调用方法起作用。
第二种方法:
在配置文件中加入
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven>
出处:
http://www.cnblogs.com/dyllove98/p/3180158.html
阅读全文
0 0
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- spring data jps
- CRC校验程序设计
- React-Native 常见问题总结
- 909422229_JSON数组与JSON对象的创建使用
- Eclipse4.4.2+scala2.11+jdk1.8+scala-plugin开发第一个scala程序
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- spark 2.1 RDD compute process
- 获取当前日期时间字符串的方法
- 对titanic.csv数据进行预测生死
- java.lang.IllegalStateException: ScrollView can host only one direct child
- 列表笔记
- 使用Redux和ngrx构建更好的Angular2应用(二)
- iOS的进阶之路
- Color the ball