8

I'm using a simple Spring Interceptor class to log all REST requests/responses for the RestTemplate object in my Android app. So far everything works fine.

public class LoggerInterceptor implements ClientHttpRequestInterceptor {

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
  ClientHttpRequestExecution execution) throws IOException {

Log.d(TAG, "Request body: " + new String(body));
// ...more log statements...

ClientHttpResponse response = execution.execute(request, body);

Log.d(TAG, "Response Headers: " + response.getHeaders());

return response;
}

At initialization I call:

List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
LoggerInterceptor loggerInterceptor = new LoggerInterceptor();
interceptors.add(loggerInterceptor);
restTemplate.setInterceptors(interceptors);

However I cannot log response.getBody() in the method above because the InputStream gets consumed once and throws an IllegalStateException when it is consumed again later. Is there a way around this so that I can log the response body too?

2 Answers 2

15

To allow for multiple response.getBody() calls wrap your ClientHttpRequestFactory in a BufferingClientHttpRequestFactory.

ClientHttpRequestFactory requestFactory = 
    new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory());
restTemplate.setRequestFactory(requestFactory);
1
  • Using the 3.1 Spring framework or later, this is definitely a more elegant solution.
    – Nachi
    Oct 20, 2014 at 8:34
6

Implement ClientHttpResponse yourself and add a setBody(byte[] body) method and override getBody. In your code above, you can then store the byte array in an ByteArrayInputStream, log what you want, then set the byte array back in the instance of your class that implements ClientHttpResponse, and that you can then return.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.