Spring ClientHttpRequestInterceptor with RestTemplate

In this Sprig boot rest interceptor example, learn to use ClientHttpRequestInterceptor with Spring RestTemplate to log request and response headers and body in Spring AOP style.

1. Spring REST Interceptor Usages

We can use this interceptor for many useful tasks.

  1. To add custom header to the response
  2. To log HTTP request and response
  3. To deny certain requests etc.

2. REST Interceptor to Log Requests and Responses

Below given RequestResponseLoggingInterceptor class implements ClientHttpRequestInterceptor interface. It implements intercept() method. In this method, we are logging the request and response details sent from RestTemplate.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StreamUtils;
 
import java.io.IOException;
import java.nio.charset.Charset;

public class RequestResponseLoggingInterceptor implements ClientHttpRequestInterceptor {
	 
    private final Logger log = LoggerFactory.getLogger(this.getClass());
 
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException 
    {
        logRequest(request, body);
        ClientHttpResponse response = execution.execute(request, body);
        logResponse(response);

        //Add optional additional headers
        response.getHeaders().add("headerName", "VALUE");

        return response;
    }
 
    private void logRequest(HttpRequest request, byte[] body) throws IOException 
    {
        if (log.isDebugEnabled()) 
        {
            log.debug("===========================request begin================================================");
            log.debug("URI         : {}", request.getURI());
            log.debug("Method      : {}", request.getMethod());
            log.debug("Headers     : {}", request.getHeaders());
            log.debug("Request body: {}", new String(body, "UTF-8"));
            log.debug("==========================request end================================================");
        }
    }
 
    private void logResponse(ClientHttpResponse response) throws IOException 
    {
        if (log.isDebugEnabled()) 
        {
            log.debug("============================response begin==========================================");
            log.debug("Status code  : {}", response.getStatusCode());
            log.debug("Status text  : {}", response.getStatusText());
            log.debug("Headers      : {}", response.getHeaders());
            log.debug("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
            log.debug("=======================response end=================================================");
        }
    }
}

3. Register ClientHttpRequestInterceptor with RestTemplate

Now add above interceptor with RestTemplate’s setInterceptors() method.

@Bean
public RestTemplate restTemplate() 
{
    RestTemplate restTemplate = new RestTemplate();

    restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(clientHttpRequestFactory()));
    restTemplate.setMessageConverters(Collections.singletonList(mappingJacksonHttpMessageConverter()));

    restTemplate.setInterceptors( Collections.singletonList(new RequestResponseLoggingInterceptor()) );

    return restTemplate;
}

Read More: RestTemplate with HttpClient

4. Demo

Start the application and execute any REST API using RestTemplate. Observe the logs.

2018-06-18T15:27:38.203+0530 DEBUG ===========================request begin=============================================
2018-06-18T15:27:38.203+0530 DEBUG URI         : <URL>
2018-06-18T15:27:38.203+0530 DEBUG Method      : POST
2018-06-18T15:27:38.203+0530 DEBUG Headers     : {Accept=[application/json], Content-Type=[application/json]}
2018-06-18T15:27:38.204+0530 DEBUG Request body: <BODY>
2018-06-18T15:27:38.204+0530 DEBUG ==========================request end================================================

2018-06-18T15:27:38.203+0530 DEBUG ===========================response begin=============================================
2018-06-18T15:27:38.203+0530 DEBUG Status code      : 200
2018-06-18T15:27:38.203+0530 DEBUG Status text      : OK
2018-06-18T15:27:38.203+0530 DEBUG Headers     		: {Accept=[application/json], Content-Type=[application/json]}
2018-06-18T15:27:38.204+0530 DEBUG Response BODY    : <BODY>
2018-06-18T15:27:38.204+0530 DEBUG ==========================response end================================================

Drop me your questions in the comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

3 thoughts on “Spring ClientHttpRequestInterceptor with RestTemplate”

  1. Don’t you think StreamUtils.copyToString(response.getBody(), Charset.defaultCharset())) is going to consume the data out of data stream and subsequent response.getBody() will not get the body ?

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.