问题:ajax 发送请求得到的数据中包含中文显示????乱码
原因:SpringMVC框架的 @RequestBody 和 @ResponseBody两个注解,分别完成请求对象到对象响应的过程,一步到位,但是因为Spring3.x以后有了HttpMessageConverter消息转换器,把返回String类型的数据编码全部默认转换成iso-8859-1的编码格式,所以就出现了我们遇到的乱码的情况,如返回list或其它则使用 MappingJacksonHttpMessageConverter。
解决办法:
1:修改 @RequestMapping中的参数produces
@RequestMapping(value = "/url.do",produces = {"text/html;charset=utf-8"})
2:不使用 @ResponseBody注解,直接通过response的方法返回数据
response.setContentType("text/html;charset=UTF-8");//这些设置必须要放在getWriter的方法之前,
response.getWriter().print(JSON.toJSONString(数据));
3:网上找到的本人没有验证是否可行,如下
<!-- 注解驱动 -->
<mvc:annotation-driven>
<!-- 指定http返回编码格式,不然返回ajax请求json会出现中文乱码 -->
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
<value>*/*;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>