interceptor prehandle contentcachingrequestwrapper请求空

发布于 2025-02-13 13:45:14 字数 1544 浏览 0 评论 0 原文

Interceptor Prehandle的请求为空。 为什么会发生这种情况?

@Component
public class CustomServletWrappingFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        ContentCachingRequestWrapper wrappingRequest = new ContentCachingRequestWrapper(httpServletRequest);
        ContentCachingResponseWrapper wrappingResponse = new ContentCachingResponseWrapper(httpServletResponse);
        filterChain.doFilter(wrappingRequest, wrappingResponse);
        wrappingResponse.copyBodyToResponse();
    }

}

预处理请求是空完

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (CallCountUtil.isMimeTypeJson(request)) {
            ContentCachingRequestWrapper cachingRequestWrapper = (ContentCachingRequestWrapper) request;
            String json = new String(cachingRequestWrapper.getContentAsByteArray());
        }
}

完成的请求。

@Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws IOException {
        if (CallCountUtil.isMimeTypeJson(request)) {
            ContentCachingRequestWrapper cachingRequestWrapper = (ContentCachingRequestWrapper) request;
            String json = new String(cachingRequestWrapper.getContentAsByteArray());
        }
}

Request from Interceptor preHandle is empty.
Why is this happening?

@Component
public class CustomServletWrappingFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        ContentCachingRequestWrapper wrappingRequest = new ContentCachingRequestWrapper(httpServletRequest);
        ContentCachingResponseWrapper wrappingResponse = new ContentCachingResponseWrapper(httpServletResponse);
        filterChain.doFilter(wrappingRequest, wrappingResponse);
        wrappingResponse.copyBodyToResponse();
    }

}

preHandle request is Empty

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (CallCountUtil.isMimeTypeJson(request)) {
            ContentCachingRequestWrapper cachingRequestWrapper = (ContentCachingRequestWrapper) request;
            String json = new String(cachingRequestWrapper.getContentAsByteArray());
        }
}

afterCompletion request is ok.

@Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws IOException {
        if (CallCountUtil.isMimeTypeJson(request)) {
            ContentCachingRequestWrapper cachingRequestWrapper = (ContentCachingRequestWrapper) request;
            String json = new String(cachingRequestWrapper.getContentAsByteArray());
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2025-02-20 13:45:15

如果您阅读您会有答案。

此类是仅在读取时缓存内容的拦截器,但否则不会导致读取内容。这意味着,如果未消费请求内容,则不会缓存内容,并且不能通过 getContentasbyTearray()

prehandle 方法中,请求尚未消耗,因此 getContentasbyTearray()是空的。在 phatercompletion 中,请求已被消耗并填充了缓存,因此 getContentAsbyTearray()确实返回值。

基本上,您不应该关心它是否是 contentCachingRequestWrapper ,只需从 httpservletrequest 读取 inputStream ,就像通常一样。

这样的东西。

String json = StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8);

If you read the javadocs you would have the answer.

This class acts as an interceptor that only caches content as it is being read but otherwise does not cause content to be read. That means if the request content is not consumed, then the content is not cached, and cannot be retrieved via getContentAsByteArray().

In the preHandle method the request hasn't been consumed yet and thus getContentAsByteArray() is empty. In the afterCompletion the request has been consumed and the cache has been filled and thus getContentAsByteArray() does return a value.

Basically you shouldn't care if it is a ContentCachingRequestWrapper, just read the InputStream from the HttpServletRequest as you normally would do.

Something like this.

String json = StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文