问题背景
后台开发的过程中,我们常常会根据@RequestParam从Form Data ,或者@PathVariable 从URL中获取参数数据。例如:
案例1:
@RequestMapping("/",method = RequestMethod.POST)
public String Demo1(@RequestParam(name="lid") int id){
System.out.println("----"+id);
return null;
}
案例2:
// restful风格的接口,请求的URL上含有路径参数,可以利用正则的方式匹配获取
@RequestMapping(value = {"/rest/AlarmUser/{userName:.*}"}, method = RequestMethod.POST)
@ResponseBody
public String updateAlarmUser(@PathVariable String userName) {
log.info("request param: userName="+userName);
//something else
当其中有中文的时候,不可避免发生了乱码。
解决方案
GET方法
方法一:修改Tomcat的server.xml
因为Tomcat默认使用ISO-8859-1进行解码的,可以将其统一设定为“utf-8”编码。
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
添加URIEncoding="utf-8"
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8"/>
方法二:手动更改
可以基于下面的语法写个工具类。
String name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
POST方法
方法一:使用Spring的字符过滤器
配置在web.xml中:
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
有人说,已经配置,但是不起作用。那么,请检查一下你的过滤器的位置,一定要放在所有其他过滤器之前。
下面举一个设置失效的案例:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>springdispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springdispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
# 放在了HiddenHttpMethodFilter的后面
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
测试了几种设置方案,直接放在springdispatcherServlet下,可以使用,也推荐这种写法。
方法二:手动更改
与GET方法类似,这里不再赘述。
参考资料