SpringMVC请求传递中文参数乱码问题记录

SpringMVC请求传递中文参数乱码问题记录


在使用springwebmvc进行http接口发布时,调用http接口时,请求参数中文乱码,总结需要进行两点操作;
1、web.xml中配置拦截器

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

    
        encoding
        /servlet/*
    

2、调整tomcat的server.xml配置文件
增加 URIEncoding=“UTF-8” 配置


以上的两个配置都必须进行配置,少一个都会导致中文乱码,经过测试对POST、GET请求都适用。

POST请求

http://localhost:8080/ui/servlet/query?name=中文啊&password=中文
    @RequestMapping(value = "/servlet/query", method = {RequestMethod.POST})
    public void doService(String name, String password, HttpServletRequest request, HttpServletResponse response) {
        mLogger.info("====name =" + name);
        mLogger.info("====password =" + password);
        String json = "1111111111111111";
        System.out.println(json);
    }

日志输出如下:

[ INFO]-[14:18:58,908] ====name =中文啊:
	[http-8080-Processor25] at com.test.http.HttpHelloController 
[ INFO]-[14:18:58,908] ====password =中文:
	[http-8080-Processor25] at com.test.http.HttpHelloController 
1111111111111111

GET请求

http://localhost:8080/ui/servlet/get_query?name=中文啊&password=中文
    @RequestMapping(value = "/servlet/get_query", method = {RequestMethod.GET})
    public void doService2(String name, String password, HttpServletRequest request, HttpServletResponse response) {
        mLogger.info("====name =" + name);
        mLogger.info("====password =" + password);
        String json = "1111111111111111";
        System.out.println(json);
    }

日志输出如下:

[ INFO]-[14:18:58,908] ====name =中文啊:
	[http-8080-Processor25] at com.test.http.HttpHelloController 
[ INFO]-[14:18:58,908] ====password =中文:
	[http-8080-Processor25] at com.test.http.HttpHelloController 
1111111111111111
[ INFO]-[14:20:47,321] ====name =中文啊:
	[http-8080-Processor25] at com.test.http.HttpHelloController 
[ INFO]-[14:20:47,321] ====password =中文:
	[http-8080-Processor25] at com.test.http.HttpHelloController 
1111111111111111

你可能感兴趣的:(笔记,http,tomcat,java)