Using ContentCachingRequestWrapper causes Empty Parameters Map

前端 未结 2 598
野的像风
野的像风 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.

提交回复
热议问题