springmvc接受前端的POST和get请求的时候出现乱码问题
一、背景
springmvc的坑还是蛮多的,项目做着做着一不小心就掉坑里了,这次的坑就是编码的问题。
首先举个2个栗子吧~
第一个例子:使用普通的Request接收参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
@RequestMapping(value = "/register_api", produces = "application/json;charset=utf-8") @ResponseBody public Map<String, Object> register(@RequestParam("image") MultipartFile image, HttpServletRequest request) throws UnsupportedEncodingException { Map<String, Object> result = new HashMap<String, Object>(); String district = new String(request.getParameter("district").getBytes("ISO8859-1"),"utf-8"); String identity = new String(request.getParameter("identity").getBytes("ISO8859-1"),"utf-8");; System.out.println(district); System.out.println(identity); ApplicationContext ac = new ClassPathXmlApplicationContext("springMVC.xml"); ocr = (OcrIdCard) ac.getBean("ocr"); register = (Register) ac.getBean("register"); try { byte[] imageBytes = image.getBytes(); boolean is_success = ocr.identification(imageBytes);
resident = ocr.set_resident(district,identity); boolean add_success = register.register(resident); if (is_success && add_success) { result.put("code", 20); } else { throw new Exception(); } } catch (Exception e) { result.put("code", 500); } return result; }
|
第二个例子:使用@RequestBody封装成实体类的例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @RequestMapping(value = "/login_api",consumes="application/json",method = RequestMethod.POST) @ResponseBody public Map<String,Object> login(@RequestBody Resident resident, HttpServletRequest request,ModelMap modelMap){ Map<String,Object> result = new HashMap<String,Object>(); ApplicationContext ac = new ClassPathXmlApplicationContext("springMVC.xml"); encrypt(resident); Login login_ = (Login) ac.getBean("login"); boolean is_success = login_.login(modelMap,resident); if (is_success){ result.put("code",12); } else{ result.put("code",-12); } return result; }
|
说明:
1.比较上述两个例子,相信细心的你肯定发现了区别,第一个例子中我采用HttpServletRequest
来接收参数,此时,接受到的中文出现了乱码,于是我是用了utf-8进行编码。
String district = new String(request.getParameter("district").getBytes("ISO8859-1"),"utf-8");
String identity = new String(request.getParameter("identity").getBytes("ISO8859-1"),"utf-8");
2.第二个例子中,我采用@Requestbody
注解的方式将参数封装成实体类,我猜想应该springmvc在底层在封装参数的时候应该为我们进行了编码,从而这个例子中中文不会出现乱码。
3.说道第一种解决乱码的方法,其实有3种,只不过博主我前两种使用后不知为何仍出现乱码,于是最终拿出了比较麻烦的方法,手动对每个参数进行编码调整。
另外两种方法在这里说明一下,以便以后遇到可以参考。
法一:
修改web.xml中的配置文件,将编码统一转化成’utf-8’的编码。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping>
|
法二:
这个方法是看网上博客的,不过有的博主说这个方法危险,有的认为这个方法有效,这里简单说明一下:就是修改tomcat安装路径下的conf文件夹中的server.xml配置文件,修改内容如下:
1 2 3
| <Connector URIEncoding="utf-8" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
|
增添URIEncoding="utf-8"
这句代码。
以上就是我对中文编码问题出现的解决方案,选择自己最合适的就是最好的!加油!