title: WebClient - how to ignore a specific HTTP error
body: <p>I'd like to create a Spring WebClient that ignores a specific HTTP error. From the documentation of WebClient.retrieve():</p>
  <blockquote>
  <p>By default, 4xx and 5xx responses result in a WebClientResponseException. To customize error handling, use ResponseSpec.onStatus(Predicate, Function) handlers.</p>
  </blockquote>
  <p>I want all calls through a WebClient instance to ignore the specific HTTP error. That is why onStatus() is of no use to me (it has to be set per response).</p>
  <p>The best I could come up with is this:</p>
  <pre><code>        WebClient webClient = WebClient.builder().filter((request, next) -&gt; {
              Mono&lt;ClientResponse&gt; response = next.exchange(request);
              response = response.onErrorResume(WebClientResponseException.class, ex -&gt; {
                  return ex.getRawStatusCode() == 418 ? Mono.empty() : Mono.error(ex);
              });
              return response;
          }).build();
  
          URI uri = UriComponentsBuilder.fromUriString(&quot;https://httpstat.us/418&quot;).build().toUri();
          webClient.get().uri(uri).retrieve().toBodilessEntity().block();
  </code></pre>
  <p>but it does throw the exception instead of ignoring it (the lambda passed to onErrorResume() is never called).</p>
  <p><strong>Edited:</strong> fixed the mistake pointed out by the first answer.</p>