Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log Spring WebClient response

I'm new to Spring WebClient. Can someone advise the best way to log REST request and response from another webservice?

I've already seen an example of logging request within the question but also have to log a response and a request for a POST call. how to log Spring 5 WebClient call

Thank you.

like image 432
ddzz Avatar asked Mar 03 '26 00:03

ddzz


1 Answers

One option is to use the onStatus function. The advantage is that you can react differently on different status codes:

.onStatus(HttpStatus::is4xxClientError, res -> {
  res.toEntity(String.class).subscribe(
    entity -> log.warn("Client error {}", entity)
   );
   return Mono.error(new HttpClientErrorException(res.statusCode()));}
 )

But be aware that this will log asynchronously, that means it might log after you already logged something different. I'm using this way right now but I know it is not perfect, so I will be happy to see better suggestions.

like image 127
Tobske Avatar answered Mar 04 '26 17:03

Tobske



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!