SpringMVC @RequestParam 中文乱码问题解决

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

http://127.0.0.1:8080/SpringMVCHibernate4/rest/user/getUserById0/100-张三
@RequestMapping(value="/getUserById0/{id}-{name}",method=RequestMethod.GET)
@ResponseBody 
public 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)
@ResponseBody
public  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配置了字符编码过滤器,但是依然是乱码:


    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
	
       forceEncoding
       true
	


    encodingFilter
    /*

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





你可能感兴趣的:(互联网开发,JAVA)