Spring 4.2.4.RELEASE MVC 学习笔记 - 6.3
本小节重点解决RestFull模式返回字符串中文乱码。我们回头看看之前写的测试URL:http://127.0.0.1:8000/user.html 页面,以及处理该页面的 UserController。
该Controller中,我们简单粗暴的让处理方法直接返回JSON字符串,通过浏览器得到结果时会有中文乱码。
造成直接返回字符串乱码的原因
据我个人分析,Controller方法直接返回JSON字符串,所以在Spring mvc在ViewResolver中没有找到任何映射的view去处理返回结果。所以Spring mvc会采用默认的HttpMessageConverter去解析返回JSON字符串并以留的形式直接response回来。所以如果response回来的字符串数据存在中文乱码,那就是HttpMessageConverter在处理的时候搞的鬼。
我们来看看spring mvc源代码,这里我不会跟读,感兴趣的可以自己去读源代码。看下面的图,看到了吗?就在那里最贵祸首,在Spring mvc中StringHttpMessageConverter解析器用来request、response过程中对传递字符串数据的解析器,它默认用的是”ISO-8859-1”。
那么好了,如果你想知道什么时候才会启用默认的字符集编码的问题,请仔细阅读spring mvc的源码吧。
PS:个人经验,我一般会从阅读属性开始,因为是spring啦,如果该类的属性存在对应setter方法,那边你就可以通过spring的注入的方式修改属性值。
解决中文乱码
方法一,配置文件方式;
配置文件的方式,配置一次所有的request、response的字符串数据解析统一处理。
在spring配置文件中,追加如下代码。(PS:我不喜欢这样处理)
/framework_spring/src/main/resources/config/spring/spring-mvc-servlet.xml
<!-- 开启spring mvc注解支持 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
方法二,注解方式;
在Controller的处理方法上,通过注解描述该请求返回数据类型。
注意
@RequestMapping(path = “/userAdd.api”, method = RequestMethod.POST,produces=”application/json; charset=UTF-8”)
其中produces=”application/json; charset=UTF-8”就描述了该方法返回的数据类型为JSON数据,字符集为UTF-8。
package cn.vfire.framework.spring.mvc.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import cn.vfire.framework.spring.mvc.mode.UserMode;
import cn.vfire.framework.spring.mvc.view.XModelAndView;
@RequestMapping("/api")
@RestController
public class UserController {
@RequestMapping(path = "/userAdd.api", method = RequestMethod.POST,produces="application/json; charset=UTF-8")
@ResponseBody
public String userAdd(UserMode user) {
XModelAndView modeView = new XModelAndView();
modeView.addObject("user", user);
System.out.println(String.format("添加用户%s完成", user.getName()));
return modeView.toJson();
}
@RequestMapping(path = "/userQuery.api", method = RequestMethod.GET)
public String userQuery(String username) {
XModelAndView modeView = new XModelAndView();
UserMode user = new UserMode();
{
user.setUsername(username);
user.setName("小红");
user.setAge(22);
user.setSex(0);
}
modeView.addObject("user", user);
System.out.println(String.format("查询用户名为%s用户 信息成功", user.getUsername()));
return modeView.toJson();
}
@RequestMapping(path = "/userSave.api", method = RequestMethod.POST)
public String userSave(UserMode user) {
XModelAndView modeView = new XModelAndView();
user.setAge(23);
modeView.addObject("user", user);
System.out.println(String.format("更新用户名为%s用户信息成功", user.getUsername()));
return modeView.toJson();
}
@RequestMapping(path = "/userDel.api", method = RequestMethod.GET)
public String userDel(String username) {
XModelAndView modeView = new XModelAndView();
System.out.println(String.format("删除用户名为%s用户信息成功", username));
return modeView.toJson();
}
}
好了,自己去测试把,无乱那种方法,中文乱码都不见了。