Je veux enregistrer l'exécution de l'API Web par le modèle de service Web
fourni par le framework Spring.
ClientInterceptor
dans WebServiceTemplate
--Utilisez un intercepteur qui fait référence à la requête / réponse de WebServiceTemplate
Intercepteur WebServiceTemplate
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
WebServiceTemplate
avec un intercepteur qui génère des journaux.@Configuration
public class ExternalApiConfig {
@Bean
WebServiceTemplate webServiceTemplate() {
return new WebServiceTemplateBuilder()
.additionalInterceptors(new WebServiceTemplateLoggingInterceptor())
.build();
}
}
Spring WS - Log Client Server HTTP Headers Example
Recommended Posts