[JAVA] Log out RestTemplate request / response

Overview

I want to log the execution of WebAPI by RestTemplate provided by Spring framework.

environment

Insert ClientHttpRequestInterceptor into RestTemplate

--Use an interceptor that intervenes in the request / response of RestTemplate

Interceptor that outputs logs

Rest Template interceptor


import lombok.extern.slf4j.Slf4j;
import lombok.val;
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.StandardCharsets;
import java.util.UUID;

@Slf4j
public class RestTemplateLoggingInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request,
                                        byte[] body,
                                        ClientHttpRequestExecution execution) throws IOException {
        val requestUuid = UUID.randomUUID();
        logRequest(requestUuid, request, body);
        val response = execution.execute(request, body);
        logResponse(requestUuid, response);
        return response;
    }

    private void logRequest(UUID requestUuid, HttpRequest request, byte[] body) {
        if (log.isInfoEnabled()) {
            log.info("[API:Request({})] Request=[{}:{}], Headers=[{}], Body=[{}]",
                requestUuid,
                request.getMethod(),
                request.getURI(),
                request.getHeaders(),
                new String(body)
            );
        }
    }

    private void logResponse(UUID requestUuid, ClientHttpResponse response) throws IOException {
        if (log.isInfoEnabled()) {
            log.info("[API:Response({})] Status=[{}:{}], Headers=[{}], Body=[{}]",
                requestUuid,
                response.getStatusCode().value(),
                response.getStatusText(),
                response.getHeaders(),
                StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8)
            );
        }
    }

}

Bean definition of RestTemplate

--Bean definition RestTemplate which set interceptor to output log. --Use BufferingClientHttpRequestFactory to avoid the problem of log output interceptor.

@Configuration
public class ExternalApiConfig {

    @Bean
    RestTemplate restTemplate() {
        return new RestTemplateBuilder()
            .requestFactory(() -> new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()))
            .additionalInterceptors(new RestTemplateLoggingInterceptor())
            .build();
    }

}

reference

Log the contents of request and response with RestTemplate

Recommended Posts

Log out RestTemplate request / response
Log output of WebServiceTemplate request / response
Output request and response log in Spring Boot