1

I am working in a spring boot application where I defined an interceptor. In the interceptor's "postHandle" method, I get a HttpServletRequest and HttpServletResponse. How can I convert the HttpServletResponse to a String?

Here is what I have tried so far:

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse res, Object o, ModelAndView mav) throws Exception {

OutputStream outputStream = res.getOutputStream();

///Convert this outputstream to String

}

This OutputStream is a ServletOutputstream. I can not convert it to BytearrayOutputStream. If i could do that, I could convert it to a String.

4
  • i want to get the content of that stream.is it possible? Commented Feb 23, 2017 at 11:30
  • perhaps with a filter... Commented Feb 23, 2017 at 11:34
  • refer below link stackoverflow.com/questions/701681/… Commented Feb 23, 2017 at 11:35
  • @Snehal Patel,i saw that question.but i am using interceptor not filter. Commented Feb 23, 2017 at 11:37

1 Answer 1

2

There are 2 options to do it .1 -

public class MyFilter implements Filter {

public void init(FilterConfig filterConfig) throws ServletException {
}

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ByteArrayPrintWriter pw = new ByteArrayPrintWriter(baos);

HttpServletResponse wrappedResp = new HttpServletResponseWrapper((HttpServletResponse) servletResponse) {
    @Override
    public PrintWriter getWriter() {
        return pw;
    }

    @Override
    public ServletOutputStream getOutputStream() {
        return new ServletOutputStream() {
            @Override
            public void write(int b) {
                baos.write(b);
            }
        };
    }
};

filterChain.doFilter(servletRequest, wrappedResp);
byte[] bytes = baos.toByteArray();
 String responseStr = new String(bytes);
servletResponse.getOutputStream().write(bytes);
}

 public void destroy() {}

 public static class ByteArrayPrintWriter extends PrintWriter {
 public ByteArrayPrintWriter(OutputStream out) {
    super(out);
 }
 }
 }

2 way is easy but work with spring-web-4*.jar version

HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
    ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);

    Instant start = Instant.now();

    filterChain.doFilter(requestWrapper, responseWrapper);

    Instant finish = Instant.now();
    long timeElapsed = Duration.between(start, finish).toMillis();

    byte[] responseArray = responseWrapper.getContentAsByteArray();
    String responseStr = new String(responseArray, responseWrapper.getCharacterEncoding());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.