HttpServletRequest getParameterNames为空

问题描述:

@RequestMapping("/notify")
public String notify(HttpServletRequest request) {
	log.info("request.getParameterNames():{}", JSON.toJSONString(request.getParameterNames()));
	Enumeration<String> parameterNames = request.getParameterNames();

日志结果:

request.getParameterNames():[]

问题分析:

请求为–表单请求,POST:【POSTMAN】
Content-Type:application/x-www-form-urlencoded
Body: 选x-www-form-urlencoded,然后填入值

请求日志的过滤器:

public class LogFilter extends OncePerRequestFilter {

	@Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        request = new RequestWrapper(request); 
        response = new ResponseWrapper(response);
        filterChain.doFilter(request, response);
    }

RequestWrapper的构造函数,调用getInputStream,导致getParameter数据量为空。

解决方案:

将request = new RequestWrapper(request); 改成request = new HttpServletRequestWrapper(request);即可。

日志输出:request.getParameterNames():["\"BankType\"",
上一篇:HttpServletResponce类


下一篇:java获取HttpServletRequest 的客户端ip