title: TestRestTemplate - Log response without destroying the body
body: <p>In order to log the request and response I have added a <strong>LoggingRequestInterceptor</strong> which implements <strong>ClientHttpRequestInterceptor</strong>... </p>
  
  <p>The response body is a stream and if I read it my interceptor it won’t be available for TestRestTemplate to deserialize it into my object model. In other words, when I call testRestTemplate.get… I’ll always get back empty objects (even as I see the object my response). </p>
  
  <p>To solve this for RestTemplate it can be fixed by using a <strong>BufferingClientHttpRequestFactory</strong>. I don't know how to fix it for TestRestTemplate though...</p>
  
  <p>I have tried to add BufferingClientHttpRequestFactory to an instance of RestTemplate and then wrap TestRestTemplate with that instance:</p>
  
  <pre><code>restTemplateBuilder.configure(restTemplate);
  </code></pre>
  
  <p>...but then there's no logging at all. </p>
  
  <p>This is how I add the logging interceptor.</p>
  
  <pre><code>public static RestTemplateBuilder withInterceptors() {
          List&lt;ClientHttpRequestInterceptor&gt; interceptors = new ArrayList&lt;&gt;();
          interceptors.add(new LoggingRequestInterceptor());
  
          return new RestTemplateBuilder()
                  .interceptors(interceptors);
      }
  </code></pre>
  
  <p>In the end I would like to:
  1. Log the response body
  2. Deserialize it into a object model (now it's null if I turn on logging)</p>