[JAVA] Log output of WebServiceTemplate request / response

Overview

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

environment

Insert ClientInterceptor into WebServiceTemplate

--Use an interceptor that refers to the request / response of WebServiceTemplate --Output the sent / received HTTP header body in the log

Interceptor that outputs logs

WebServiceTemplate interceptor


import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.ws.client.WebServiceClientException;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.transport.TransportOutputStream;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

@Slf4j
public class WebServiceTemplateLoggingInterceptor implements ClientInterceptor {

    private final ThreadLocal<UUID> requestUuid = new ThreadLocal<>();

    @SneakyThrows
    @Override
    public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
        if (log.isInfoEnabled()) {
            requestUuid.set(UUID.randomUUID());
            val transportOutputStream = new ByteArrayTransportOutputStream();
            messageContext.getRequest().writeTo(transportOutputStream);
            log.info("[SOAP:Request({})] Headers=[{}], Body=[{}]",
                requestUuid.get(),
                transportOutputStream.getHeaders(),
                transportOutputStream.getBody()
            );
        }
        return true;
    }

    @SneakyThrows
    @Override
    public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
        if (log.isInfoEnabled()) {
            val transportOutputStream = new ByteArrayTransportOutputStream();
            messageContext.getResponse().writeTo(transportOutputStream);
            log.info("[SOAP:Response({})] Headers=[{}], Body=[{}]",
                requestUuid.get(),
                transportOutputStream.getHeaders(),
                transportOutputStream.getBody()
            );
        }
        return true;
    }

    @Override
    public boolean handleFault(MessageContext messageContext) throws WebServiceClientException {
        return true;
    }

    @Override
    public void afterCompletion(MessageContext messageContext, Exception ex) throws WebServiceClientException {
    }

    private static class ByteArrayTransportOutputStream extends TransportOutputStream {
        private final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        @Getter
        private final MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();

        @Override
        public void addHeader(String name, String value) throws IOException {
            headers.add(name, value);
        }

        @Override
        protected OutputStream createOutputStream() throws IOException {
            return byteArrayOutputStream;
        }

        public String getBody() {
            return new String(byteArrayOutputStream.toByteArray());
        }
    }

}

Bean definition of WebServiceTemplate

--Bean definition WebServiceTemplate with interceptor to output log.

@Configuration
public class ExternalApiConfig {

    @Bean
    WebServiceTemplate webServiceTemplate() {
        return new WebServiceTemplateBuilder()
            .additionalInterceptors(new WebServiceTemplateLoggingInterceptor())
            .build();
    }

}

reference

Spring WS - Log Client Server HTTP Headers Example

Recommended Posts

Log output of WebServiceTemplate request / response
Output request and response log in Spring Boot
Log out RestTemplate request / response
Output HTTP header of google-http-client to log
[Apache Camel] Easy output of throughput to log
Request parameter log output sample Java & Spring MVC
About Java log output
Format of the log output by Tomcat itself in Tomcat 8
Output in multiples of 3
I want to change the log output settings of UtilLoggingJdbcLogger
Control log output with Doma2
Java standard log output sample
Summary of 2020 programming learning output