Created
August 15, 2016 11:48
-
-
Save baberali/59ad73b2a1dcc5042e53f32585770df7 to your computer and use it in GitHub Desktop.
Instead of writing your own classes to cache request response for logging, Spring provides a couple of useful classes i.e. ContentCachingRequestWrapper and ContentCachingResponseWrapper. These classes can be utilized very effectively, for example, in the following little filter:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import java.util.Date; | |
import javax.servlet.Filter; | |
import javax.servlet.FilterChain; | |
import javax.servlet.FilterConfig; | |
import javax.servlet.ServletException; | |
import javax.servlet.ServletRequest; | |
import javax.servlet.ServletResponse; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.stereotype.Component; | |
import org.springframework.web.util.ContentCachingRequestWrapper; | |
import org.springframework.web.util.ContentCachingResponseWrapper; | |
import org.springframework.web.util.WebUtils; | |
@Component | |
public class LoggingFilter implements Filter { | |
private static final Logger LOG = LoggerFactory.getLogger(LoggingFilter.class); | |
@Override | |
public void init(FilterConfig filterConfig) throws ServletException { | |
} | |
@Override | |
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) | |
throws IOException, ServletException { | |
if (servletRequest instanceof HttpServletRequest | |
&& servletResponse instanceof HttpServletResponse) { | |
HttpServletRequest request = (HttpServletRequest) servletRequest; | |
HttpServletResponse response = (HttpServletResponse) servletResponse; | |
HttpServletRequest requestToCache = new ContentCachingRequestWrapper(request); | |
HttpServletResponse responseToCache = new ContentCachingResponseWrapper(response); | |
chain.doFilter(requestToCache, responseToCache); | |
String requestData = getRequestData(requestToCache); | |
String responseData = getResponseData(responseToCache); | |
} else { | |
chain.doFilter(servletRequest, servletResponse); | |
} | |
} | |
private static String getRequestData(final HttpServletRequest request) throws UnsupportedEncodingException { | |
String payload = null; | |
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); | |
if (wrapper != null) { | |
byte[] buf = wrapper.getContentAsByteArray(); | |
if (buf.length > 0) { | |
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding()); | |
} | |
} | |
return payload; | |
} | |
private static String getResponseData(final HttpServletResponse response) throws IOException { | |
String payload = null; | |
ContentCachingResponseWrapper wrapper = | |
WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class); | |
if (wrapper != null) { | |
byte[] buf = wrapper.getContentAsByteArray(); | |
if (buf.length > 0) { | |
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding()); | |
wrapper.copyBodyToResponse(); | |
} | |
} | |
return payload; | |
} | |
@Override | |
public void destroy() { | |
} | |
} |
It was really helpful. thank you
@ozeta Thats what seems to be the case. When I try these in interceptors it does return empty wrapper. Works only in filters.
Thanks, was truly helpful.
Calling getRequestData(requestToCache); before filter chain gives a null body.
Thanks, this code solved all my problems to log response body in my project.
Hello guys, I tried the same but still giving me null in both requestData and responseData
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I'm using getRequestData into my Spring 4 Interceptor class, but getNativeRequest returns always an empty wrapper. This method can be used only on filters?