前提,tomcat的server.xml中已经添加URIEncoding="UTF-8",问题依然。
(1)使用arthas,在controller方法上监控,查看参数 watch cn.xxx.yyy.controller.zzzzController export "{params,returnObj}" -x 2 发现进来参数,中文就是问号。
(2)怀疑URIEncoding="UTF-8"参数没生效吗? 在web.xml中添加Filter,将request参数输出看看。
package cn.xxx.yyy;
import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.Arrays; import java.util.Map;
// 创建过滤器类 public class RequestLoggingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException {
}
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
// 打印请求URL和方法 System.out.println("请求URL: " + httpRequest.getRequestURL()); System.out.println("请求方法: " + httpRequest.getMethod());
// 打印所有参数 System.out.println("请求参数:"); Map<String, String[]> map = httpRequest.getParameterMap(); for(String key :map.keySet()){ System.out.print(" " + key + ": "); String[] values = map.get(key); for(String v:values){ System.out.print(v + " "); } System.out.println(); }
chain.doFilter(request, response); }
@Override public void destroy() {
} }
<filter> <filter-name>requestLoggingFilter</filter-name> <filter-class>cn.xxx.yyy.RequestLoggingFilter</filter-class> </filter> <filter-mapping> <filter-name>requestLoggingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<filter> <filter-name>requestLoggingFilter2</filter-name> <filter-class>cn.xxx.yyy.RequestLoggingFilter</filter-class> </filter> <filter-mapping> <filter-name>requestLoggingFilter2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
由于有多个Filter,所以我也加了多个Filter,name不同而已 这样可以确认,是否哪个filter影响了解码。 查看日志,发现经过Filter时,参数里的中文都是正常的。
(3)filter之后,就是springmvc的内容了,查看堆栈
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:946) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:880) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) at javax.servlet.http.HttpServlet.service(HttpServlet.java:529)
发现InvocableHandlerMethod类里有trace日志, 通过log4j添加 log4j.logger.org.springframework.web.method.HandlerMethod=TRACE,ConsoleAppender 成功输出了日志,发现参数里中文已经是问号。
(4)怀疑是request参数转controller方法参数时出错的。 查看springmvc配置, <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="customArgumentResolvers"> <list> <bean class="cn.xxx.yyyy.CrudHandlerMethodArgumentResolver"/> </list> </property> </bean> 发现自定义了一个参数转换器,里面iso转utf8导致的,呵呵。
|