SexyMF
SexyMF

Reputation: 11185

Filter response returning only part of the content

I have the below method that extracts the response and wraps it in a wrapper class.

With the debugger, I can see that responseContent has the full content as it should be, but when it is printed to the screen, only part of the request is returning.

I can see that the variable data has the entire value as it should have.

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper((HttpServletResponse) response);
    chain.doFilter(request, responseWrapper);
    String responseContent = new String(responseWrapper.getContentAsByteArray());
    BasicResponse<Object> fullResponse = new BasicResponse<>();
    fullResponse.setData(new ObjectMapper().readValue(responseContent, Object.class));
    String data = new ObjectMapper().writeValueAsString(fullResponse);
    response.getWriter().write(data);//data has the entire value
    response.getWriter().close();
}

Upvotes: 3

Views: 65

Answers (1)

Karol Dowbecki
Karol Dowbecki

Reputation: 44970

Most likely the response Content-Length header is set with value lower than the new data length. Make sure to set this header with new value which matches the new data length.

Upvotes: 1

Related Questions