Given an exchange using WebClient
, filtered by a custom ExchangeFilterFunction
:
@Override
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
return next.exchange(request)
.doOnSuccess(response -> {
// ...
});
}
Trying to access the response body more than once using response.bodyToMono()
will cause the underlying HTTP client connector to complain that only one receiver is allowed. AFAIK, there's no way to access the body's Publisher in order to cache()
its signals (and I'm not sure it'd be a good idea, resource-wise), as well as no way to mutate or decorate the response
object in a manner that allows access to its body (like it's possible with ServerWebExchange
on the server side).
That makes sense, but I am wondering if there are any ways I could subscribe to the response body's publisher from a form of filter such as this one. My goal is to log the request/response being sent/received by a given WebClient
instance.
I am new to reactive programming, so if there are any obvious no-nos here, please do explain :)
no
. What you want to do goes against the reactive stack. The best approach here would be to log a request/response body explicitly in reactive chain e.g..doOnNext(body -> log.info(JsonUtil.objectAsString(body)))
for more details please pay attention to this question #45240505 – Penetrating