1. 定义ParameterRequestWrapper 继承HttpServletRequestWrapper
public class ParameterRequestWrapper extends HttpServletRequestWrapper { private Map<String,String[]> params; public ParameterRequestWrapper(HttpServletRequest request, Map<String,String[]> newParams) { super(request); this.params = newParams; } @Override public String getParameter(String name) { Object v = params.get(name); if (v == null) { return null; } else if (v instanceof String[]) { String[] strArr = (String[]) v; if (strArr.length > 0) { return strArr[0]; } else { return null; } } else if (v instanceof String) { return (String) v; } else { return v.toString(); } } @Override public Map getParameterMap() { return params; } @Override public Enumeration getParameterNames() { return new Vector(params.keySet()).elements(); } @Override public String[] getParameterValues(String name) { Object v = params.get(name); if (v == null) { return null; } else if (v instanceof String[]) { return (String[]) v; } else if (v instanceof String) { return new String[] { (String) v }; } else { return new String[] { v.toString() }; } } }
2. 在filter 中编写如下代码:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; Map<String, String[]> parametersMap = DataFormat .formatRequestParameters(httpRequest); ParameterRequestWrapper requestWrapper = new ParameterRequestWrapper( httpRequest, parametersMap); chain.doFilter(requestWrapper, response); }