尝试了所有答案,但其中一些不符合我的需求或只是不起作用。根据这个答案编写了我自己的解决方案,以拦截请求/响应正文并记录它们。
@Slf4j
@Component
public class LoggingCustomizer implements WebClientCustomizer {
@Override public void customize(WebClient.Builder webClientBuilder) {
webClientBuilder.filter((request, next) -> {
logRequest(request);
return next
.exchange(interceptBody(request))
.doOnNext(this::logResponse)
.map(this::interceptBody);
});
}
private ClientRequest interceptBody(ClientRequest request) {
return ClientRequest.from(request)
.body((outputMessage, context) -> request.body().insert(new ClientHttpRequestDecorator(outputMessage) {
@Override public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
return super.writeWith(Mono.from(body)
.doOnNext(dataBuffer -> logRequestBody(dataBuffer)));
}
}, context))
.build();
}
private ClientResponse interceptBody(ClientResponse response) {
return response.mutate()
.body(data -> data.doOnNext(this::logResponseBody))
.build();
}
private void logRequest(ClientRequest request) {
log.debug("DOWNSTREAM REQUEST: METHOD {}, URI: {}, HEADERS: {}", request.method(), request.url(), request.headers());
}
private void logRequestBody(DataBuffer dataBuffer) {
log.debug("DOWNSTREAM REQUEST: BODY: {}", dataBuffer.toString(StandardCharsets.UTF_8));
}
private void logResponse(ClientResponse response) {
log.debug("DOWNSTREAM RESPONSE: STATUS: {}, HEADERS: {}", response.rawStatusCode(), response.headers().asHttpHeaders());
}
private void logResponseBody(DataBuffer dataBuffer) {
log.debug("DOWNSTREAM RESPONSE: BODY: {}", dataBuffer.toString(StandardCharsets.UTF_8));
}
}
更新:添加了用于记录的代码段(首选解决方案)reactor.netty.http.client.HttpClient
@Slf4j
@Component
public class LoggingCustomizer implements WebClientCustomizer {
@Override public void customize(WebClient.Builder webClientBuilder) {
HttpClient httpClient = HttpClient.create()
.doOnRequest((httpClientRequest, connection) -> connection.addHandlerFirst(new LoggingHandler()));
webClientBuilder.clientConnector(new ReactorClientHttpConnector(httpClient));
}
private static class LoggingHandler extends ChannelDuplexHandler {
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof FullHttpRequest request) {
log.debug("DOWNSTREAM REQUEST: METHOD: {}, URI: {}, BODY: {}, HEADERS: {}",
request.method(), request.uri(), request.content().toString(defaultCharset()), request.headers());
} else if (msg instanceof HttpRequest request) {
log.debug("DOWNSTREAM REQUEST: METHOD: {}, URI: {}, HEADERS: {}",
request.method(), request.uri(), request.headers());
} else if (msg instanceof FullHttpMessage message) {
log.debug("DOWNSTREAM REQUEST: BODY: {}",
message.content().toString(defaultCharset()));
}
super.write(ctx, msg, promise);
}
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpResponse response) {
log.debug("DOWNSTREAM RESPONSE: STATUS: {}, BODY: {}, HEADERS: {}",
response.status().code(), response.content().toString(defaultCharset()), response.headers());
} else if (msg instanceof HttpResponse response) {
log.debug("DOWNSTREAM RESPONSE: STATUS: {}, HEADERS: {}",
response.status().code(), response.headers());
} else if (!(msg instanceof LastHttpContent) && msg instanceof HttpContent httpContent) {
log.debug("DOWNSTREAM RESPONSE: BODY: {}",
httpContent.content().toString(defaultCharset()));
}
super.channelRead(ctx, msg);
}
}
}