Using ContentCachingRequestWrapper causes Empty Parameters Map

前端 未结 2 568
野的像风
野的像风 2021-01-14 18:12

I have implemented a Filter, in which I want to read the content of request first for some checks and then I would like to go on.

But the problem is, that in the fol

相关标签:
2条回答
  • 2021-01-14 19:02

    ContentCachingRequestWrapper doesnt work that way and has some limitations. Only POST request and content type should be application/x-www-form-urlencoded as far as I remember. If this fits for you, here's what you should do:

    final HttpServletRequest req = (HttpServletRequest) request;
    HttpServletRequest servletRequest = new ContentCachingRequestWrapper(req);
    servletRequest.getParameterMap(); // needed for caching!!
    
    String read = ByteSource.wrap(servletRequest.getContentAsByteArray())
        .asCharSource(StandardCharsets.UTF_8).read(); // Please note that we're not touching input stream!!
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-14 19:05

    Please try the two proposed solutions mentioned below:
    1. HttpServletRequestWrapper servletRequest = new ContentCachingRequestWrapper(req);
    OR
    2. ContentCachingRequestWrapper servletRequest = new ContentCachingRequestWrapper(req);
    Instead of HttpServletRequest servletRequest = new ContentCachingRequestWrapper(req);
    As you can check here that ContentCachingRequestWrapper class extends HttpServletRequestWrapper which extends ServletRequestWrapper and implements HttpServletRequest.
    So here by performing upcasting, you are may be facing this issue. Please check and let me know if this is not the case, then I will debug it further.

    0 讨论(0)
提交回复
热议问题