Saturday, 18 April 2020

Spring ClientHttpRequestInterceptor with RestTemplate

Learn to use ClientHttpRequestInterceptor with Spring RestTemplate to log request and response headers and body in Spring AOP style.

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.

1) Interceptor to log request and response

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.
RequestResponseLoggingInterceptor.java
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=================================================");
        }
    }
}

2) Register ClientHttpRequestInterceptor with RestTemplate

Now add above interceptor with RestTemplate’s setInterceptors() method.
RestTemplateConfig.java
@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;
}

Demo

Start the application and execute any REST API using RestTemplate. Observe the logs.
Console
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 comments section.

No comments:

Post a Comment