I want to read the response body in my interceptor for logging.After I searched some answers, I tend to use ContentCachingResponseWrapper
.here is my code:
public class CustomerInterceptor extends HandlerInterceptorAdapter{
public void poseHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
byte[] contentAsByteArray = responseWrapper.getContentAsByteArray();
...
}
But when I debug it .I got responseWrapper with content "".But In the postman I get the response body. Then I go to the source code.
private final FastByteArrayOutputStream content = new FastByteArrayOutputStream(1024);
public ContentCachingResponseWrapper(HttpServletResponse response) {
super(response);
}
It seems that I didn't init the content.I know something is wrong in my code.My question is how to use ContentCachingResponseWrapper
correctly?