SpringMVC 使用@ResponseBody返回json非常方便,出现乱码解决办法如下
方法一:使用(produces = “application/json; charset=utf-8”):
@RequestMapping(value="/getTestByPage",produces = "application/json; charset=utf-8")
// @RequestMapping("/getTestByPage")
@ResponseBody
public String getTestByPage(String page,String rows,String text,HttpServletRequest request,HttpServletResponse response){
//业务逻辑
}
方法二,在spring-mvc.xml中添加:
<!-- 处理请求返回json字符串的中文乱码问题 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
本文介绍在SpringMVC框架中使用@ResponseBody返回JSON数据时遇到的中文乱码问题及解决方案。提供两种有效方法:一是通过@RequestBody注解设置字符集;二是配置spring-mvc.xml文件中的message-converters,确保中文字符正确编码。
1833

被折叠的 条评论
为什么被折叠?



