ddzz
ddzz

Reputation: 269

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.

Upvotes: 3

Views: 11020

Answers (1)

Tobske
Tobske

Reputation: 558

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.

Upvotes: 4

Related Questions