get ServletResponse content string for logging
<p>I have a RESTful service developed by Java Spring. I created some filters for authentications, get some custom headers, etc. etc. in one of the filters I need to log incoming Request and outgoing Response. Right now I stuck in logging the response. here is the filter </p>

<pre><code>public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException 
</code></pre>

<p><code>servletResponse</code> is the object that I need to get the content. </p>

<p>I know there are some duplicate questions that already answered but for some reason none of those works for me. </p>

<p>I have a <code>filter</code> and in <code>doFilter</code> method after <code>chain.doFilter</code> I want to log the response content. </p>

<p>I can see the <code>JSON</code> object when I'm inspecting the <code>response</code> in <code>outputStream</code> or in <code>writer</code>. But I cannot get it programmatically. </p>

<p>Can someone help me to resolve this? Thank you in advance!</p>

<p>Here are the links that I used and didn't help: </p>

<p><a href="https://stackoverflow.com/questions/8933054/how-to-log-response-content-from-a-java-web-server">How to read and copy the HTTP servlet response output stream content for logging</a></p>

<p><a href="https://stackoverflow.com/questions/3242236/capture-and-log-the-response-body">Capture and log the response body</a></p>

<p><a href="https://stackoverflow.com/questions/2158647/logging-response-body-html-from-httpservletresponse-using-spring-mvc-handlerin">Logging response body (HTML) from HttpServletResponse using Spring MVC HandlerInterceptorAdapter</a></p>