1

I am using a Spring 5 WebClient to interact with a REST server. I understand how to handle the case of a 4xx or 5xx error returned by the server. What I don't understand is how to handle the case of a non-response, for example when the server is down or when the server URL is incorrect. How should I configure the WebClient? It must handle a WebClientRequestException that wraps an UnknownHostException.

WebClient webClient = WebClient.builder()
    .baseUrl(someServerUrl)
    .build();
                
webClient
    .get()
    .uri("/api/something")
    .retrieve()
    .onStatus(HttpStatus::is4xxClientError, errorHandlerFor4xx)
    .onStatus(HttpStatus::is5xxServerError, errorHandlerFor5xx)
    .bodyToMono(SomeStuff.class)

Does anybody have a clue?

Ivan dal Bosco
  • 3,233
  • 4
  • 19
  • 24

0 Answers0