方法一、继承 HttpServletRequestWrapper , 实现自定义 request
1、除了修改的参数,其他 Header 等参数不变, 等同于修改了请求参数
2、实质是另一个请求
/** * @author zxguan * @description * @create 2018-03-02 9:51 */ public class ParameterRequestWrapper extends HttpServletRequestWrapper { private Map params; public ParameterRequestWrapper(HttpServletRequest request, Map paramMap) { super(request); params = paramMap; } @Override public Map<String, String[]> getParameterMap() { return params; } }
//这里用 new HashMap<>重新生成一个Map,因为 request.getParameterMap() 获取的参数列表不允许修改【java.lang.IllegalStateException: No modifications are allowed to a locked ParameterMap】 Map<String, String[]> paramMap = new HashMap<>(request.getParameterMap()); //xxx为要覆盖的参数, valueStr为真实参数值, wraprequest为新生成的自定义请求 paramMap.put("xxx", new String[]{valueStr}); ParameterRequestWrapper wrapRequest = new ParameterRequestWrapper(request, paramMap);
方法二、没找到 ! - _ - !