SpringMVC @RequestParam 中文乱码问题解决

   客户端用GET方法请求SpringMVC时,如果用采用如下方式则中文没有出现乱码:

//http://127.0.0.1:8080/SpringMVCHibernate4/rest/user/getUserById0/100-张三
@RequestMapping(value="/getUserById0/{id}-{name}",method=RequestMethod.GET)
public @ResponseBody String getUserById0(@PathVariable Long id, 
                                @PathVariable("name")String userName) {
	User user = new User();
	user.setUserName(userName);
	user.setId(id);
		
	return GsonUtil.toJson(user);
}

   但是如果采用如下方式则中文会出现乱码:

//http://127.0.0.1:8080/SpringMVCHibernate4/rest/user/getUserById2?id=100&name=张三
@RequestMapping(value="/getUserById2",method=RequestMethod.GET)
public @ResponseBody String getUserById2(@RequestParam Long id, 
                        @RequestParam("name") String userName) {
    User user = new User();
    user.setUserName(userName);//乱码
    user.setId(id);
		
    return GsonUtil.toJson(user);
}

   web.xml配置了字符编码过滤器,但是依然是乱码:

<filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

   最终解决办法:修改tomcat默认编码(ISO-8859-1)方法,修改tomcat根目录的conf下的server.xml,Connector元素添加URIEncoding="UTF-8"属性,如下所示:

 <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" 
                                         redirectPort="8443" URIEncoding="UTF-8" />

   以上测试基于64Bit Java1.7.0_67,Spring3.2.14.RELEASE。

你可能感兴趣的:(springMVC,中文乱码,RequestParam)