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
ReplyDeleteThe article presents a practical solution for troubleshooting Spring Boot REST clients by using ClientHttpRequestInterceptor to capture both sides of a RestTemplate exchange. Overriding the intercept method and logging the request before execution and the response afterward provides useful visibility into the URI, HTTP method, headers, request body, status code, and status text when diagnosing unexpected API behavior.
ReplyDeleteThis kind of request-response logging is particularly useful when a frontend depends on a Spring Boot backend and an API call is not behaving as expected. A Spring Boot Angular Full Stack Developer Course can help developers understand how REST communication, interceptors, and backend API behavior fit together with an Angular application.
The interceptor approach also keeps the diagnostic logic separate from the business logic of the REST client, making it easier to add or adjust logging without changing every API call. When integrating Spring Boot services with modern frontend applications, this separation can make troubleshooting considerably more systematic. A Spring Boot ReactJS Full Stack Online Course is relevant for learning this broader full-stack integration pattern with React-based applications.
ReplyDelete