3

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.

ddzz
  • 259
  • 1
  • 3
  • 10
  • So far didn't find a better way than just to decode it into String, log, and then deserialize – ddzz May 22 '18 at 14:57

1 Answers1

4

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.

Tobske
  • 518
  • 3
  • 12