Recently when working on a rest client using the Spring's RestTemplate, I needed to log the request and response as I had no clue what was wrong with my code.
Spring provides the option to write your own Interceptors. For this specific requirement, Spring provides the ClientHttpRequestInterceptor interface. I had to override its intercept method and implement my logic there. Below is the snapshot of my code
public class MyInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] body, ClientHttpRequestExecution execution) throws IOException {
printRequest(httpRequest, body);
ClientHttpResponse response = execution.execute(httpRequest, body);
printResponse(response);
return response;
}
private void printRequest(HttpRequest httpRequest, byte[] body) throws IOException {
System.out.println("URI = "+ httpRequest.getURI());
System.out.println("Method = "+ httpRequest.getMethod());
System.out.println("Headers = "+ httpRequest.getHeaders());
System.out.println("Request body= "+ new String(body, "UTF-8"));
}
private void printResponse(ClientHttpResponse response) throws IOException {
System.out.println("Status code = " + response.getStatusCode());
System.out.println("Status text = "+ response.getStatusText());
System.out.println("Headers = "+ response.getHeaders());
System.out.println("Response body= "+ StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
}
}
restTemplate.setInterceptors(Collections.singletonList(new MyInterceptor ()));
Printing the details of request and response help me to figure out what was wrong with my Request call but I was getting the java.io.IOException: Attempted read from closed stream when processing the Response again in my code. As my issue was resolved, I turned off the interceptor for RestTemplate.
Spring provides the option to write your own Interceptors. For this specific requirement, Spring provides the ClientHttpRequestInterceptor interface. I had to override its intercept method and implement my logic there. Below is the snapshot of my code
public class MyInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] body, ClientHttpRequestExecution execution) throws IOException {
printRequest(httpRequest, body);
ClientHttpResponse response = execution.execute(httpRequest, body);
printResponse(response);
return response;
}
private void printRequest(HttpRequest httpRequest, byte[] body) throws IOException {
System.out.println("URI = "+ httpRequest.getURI());
System.out.println("Method = "+ httpRequest.getMethod());
System.out.println("Headers = "+ httpRequest.getHeaders());
System.out.println("Request body= "+ new String(body, "UTF-8"));
}
private void printResponse(ClientHttpResponse response) throws IOException {
System.out.println("Status code = " + response.getStatusCode());
System.out.println("Status text = "+ response.getStatusText());
System.out.println("Headers = "+ response.getHeaders());
System.out.println("Response body= "+ StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
}
}
restTemplate.setInterceptors(Collections.singletonList(new MyInterceptor ()));
Printing the details of request and response help me to figure out what was wrong with my Request call but I was getting the java.io.IOException: Attempted read from closed stream when processing the Response again in my code. As my issue was resolved, I turned off the interceptor for RestTemplate.
Hello,
ReplyDeleteHow do you solve this problem: "Attempted read from closed stream". I'm facing the same one.
Response stream is already read while logging it, reading it again throws this exception. I have not explored it myself.
ReplyDeleteAppreciate yoour blog post
ReplyDelete