I want to log the execution of WebAPI by WebServiceTemplate
provided by Spring framework.
ClientInterceptor
into WebServiceTemplate
--Use an interceptor that refers to the request / response of WebServiceTemplate
--Output the sent / received HTTP header body in the log
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());
}
}
}
WebServiceTemplate
--Bean definition WebServiceTemplate
with interceptor to output log.
@Configuration
public class ExternalApiConfig {
@Bean
WebServiceTemplate webServiceTemplate() {
return new WebServiceTemplateBuilder()
.additionalInterceptors(new WebServiceTemplateLoggingInterceptor())
.build();
}
}
Spring WS - Log Client Server HTTP Headers Example
Recommended Posts