java - 配置springmvc字符过滤器,收到请求的中文依旧是乱码该如何解决?
问题描述
<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>
用postman输入中文参数调用,收到的都是乱码,必须手动把参数从iso8859-1转成utf-8
问题解答
回答1:试下 修改servlet容器的编码设置 tomcat的话是server.xml,印象中
回答2:1.tomcat的server.xml配置
<Connector port='8080' protocol='HTTP/1.1' connectionTimeout='20000' redirectPort='8443' URIEncoding='UTF-8' useBodyEncodingForURI='true' />
2.继承HttpServletRequestWrapper重写getParameter方法,在这个方法里面做字符串的编码转换