I’ve created a filter to in my java webserver (appengine actually) that logs the parameters of an incoming request. I’d also like to log the resulting response that my webserver writes. Although I have access to the response object, I’m not sure how to get the actual string/content response out of it.
Any ideas?
You need to create a
Filterwherein you wrap theServletResponseargument with a customHttpServletResponseWrapperimplementation wherein you override thegetOutputStream()andgetWriter()to return a customServletOutputStreamimplementation wherein you copy the written byte(s) in the base abstractOutputStream#write(int b)method. Then, you pass the wrapped customHttpServletResponseWrapperto theFilterChain#doFilter()call instead and finally you should be able to get the copied response after the the call.In other words, the
Filter:The custom
HttpServletResponseWrapper:The custom
ServletOutputStream: