using Spring WebClient (Spring boot 2.7.x) to POST messages to external endpoint.
In some cases getting readtimeout exception (wrapped under WebClientRequestException) from endpoint. Trying few ways to gracefully handle, when there is no response from server, by creating custom response for audit purposes (sample code below). In this flow trying to capture the URL, headers etc, but unable to do so.
this.webClient
.post().uri("/abc.com/sample")
.header(CONTENT_TYPE, APPLICATION_JSON_VALUE)
.body(Mono.just(xyz), XYZ.class)
.exchangeToMono(response -> Mono.just(response.mutate().build())
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))))
.onErrorResume((throwable) -> {
if(throwable instanceof WebClientRequestException){
log.error("posting data for endpoint resulted in error:{}", throwable.getMessage());
ClientResponse fallbackResponse = ClientResponse.create(HttpStatus.GATEWAY_TIMEOUT, ExchangeStrategies.withDefaults())
.build(); // TRYING TO CAPTURE & ADD POSTED PAYLOAD/URL etc
return Mono.just(fallbackResponse);
}
return Mono.error(throwable);
});
Appreciate any thoughts / suggestions?