How does ContentCachingResponseWrapper work?

Viewed 15

I understand that with ContentCachingResponseWrapper, the body of the response is cached and that can be read multiple times. But the function getContentAsByteArray() returns empty unless the chain.doFilter() method is called. I don't have another filter in my code, so I don't understand why this is the case. For example my code is -

 @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        System.out.println("In ServletFilter");
        HttpServletResponse res = (HttpServletResponse) response;
        ContentCachingResponseWrapper ccrw= new ContentCachingResponseWrapper(res);
        

        chain.doFilter(request, ccrw); // If this statement is not executed then content is empty

        String content=new String(ccrw.getContentAsByteArray(), "utf-8");

        System.out.println(content); // This prints empty if the chain.doFilter is not executed
    }
0 Answers
Related