Skip to content

Commit 2ca93cb

Browse files
committed
Update Javadoc for WebClient onStatus handlers
Closes gh-24736
1 parent 116a256 commit 2ca93cb

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientResponse.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -197,9 +197,9 @@ public interface ClientResponse {
197197
Mono<ResponseEntity<Void>> toBodilessEntity();
198198

199199
/**
200-
* Creates a {@link WebClientResponseException} based on the status code,
201-
* headers, and body of this response as well as the corresponding request.
202-
* @return a {@code Mono} with a {@code WebClientResponseException} based on this response
200+
* Create a {@link WebClientResponseException} that contains the response
201+
* status, headers, body, and the originating request.
202+
* @return a {@code Mono} with the created exception
203203
* @since 5.2
204204
*/
205205
Mono<WebClientResponseException> createException();

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -650,38 +650,39 @@ <T, P extends Publisher<T>> RequestHeadersSpec<?> body(P publisher,
650650
interface ResponseSpec {
651651

652652
/**
653-
* Register a custom error function that gets invoked when the given {@link HttpStatus}
654-
* predicate applies. Whatever exception is returned from the function (possibly using
655-
* {@link ClientResponse#createException()}) will also be returned as error signal
656-
* from {@link #bodyToMono(Class)} and {@link #bodyToFlux(Class)}.
657-
* <p>By default, an error handler is registered that returns a
658-
* {@link WebClientResponseException} when the response status code is 4xx or 5xx.
659-
* To override this default (and return a non-error response from {@code bodyOn*}), register
660-
* an exception function that returns an {@linkplain Mono#empty() empty} mono.
661-
* <p><strong>NOTE:</strong> if the response is expected to have content,
662-
* the exceptionFunction should consume it. If not, the content will be
663-
* automatically drained to ensure resources are released.
664-
* @param statusPredicate a predicate that indicates whether {@code exceptionFunction}
665-
* applies
666-
* @param exceptionFunction the function that returns the exception
653+
* Provide a function to map specific error status codes to an error
654+
* signal to to be propagated downstream instead of the response.
655+
* <p>By default, if there are not matching status handlers, responses
656+
* with status codes >= 400 are mapped to
657+
* {@link WebClientResponseException} which is created with
658+
* {@link ClientResponse#createException()}.
659+
* <p>To suppress the treatment of a status code as an error and process
660+
* it as a normal response, return {@code Mono.empty()} from the function.
661+
* The response will then propagate downstream for processing.
662+
* <p>To ignore an error response, handle it earlier with a
663+
* {@link ExchangeFilterFunction filter}, or add {@code onErrorResume}
664+
* downstream, for example:
665+
* <pre class="code">
666+
* webClient.get()
667+
* .uri("http://abc.com/account/123")
668+
* .retrieve()
669+
* .bodyToMono(Account.class)
670+
* .onErrorResume(WebClientResponseException.class,
671+
* ex -> ex.getRawStatusCode() == 404 ? Mono.empty() : Mono.error(ex));
672+
* </pre>
673+
* @param statusPredicate to match responses with
674+
* @param exceptionFunction to map the response to an error signal
667675
* @return this builder
668676
* @see ClientResponse#createException()
669677
*/
670678
ResponseSpec onStatus(Predicate<HttpStatus> statusPredicate,
671679
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction);
672680

673681
/**
674-
* Register a custom error function that gets invoked when the given raw status code
675-
* predicate applies. The exception returned from the function will be returned from
676-
* {@link #bodyToMono(Class)} and {@link #bodyToFlux(Class)}.
677-
* <p>By default, an error handler is registered that throws a
678-
* {@link WebClientResponseException} when the response status code is 4xx or 5xx.
679-
* @param statusCodePredicate a predicate of the raw status code that indicates
680-
* whether {@code exceptionFunction} applies.
681-
* <p><strong>NOTE:</strong> if the response is expected to have content,
682-
* the exceptionFunction should consume it. If not, the content will be
683-
* automatically drained to ensure resources are released.
684-
* @param exceptionFunction the function that returns the exception
682+
* Variant of {@link #onStatus(Predicate, Function)} that works with
683+
* raw status code values. This is useful for custom status codes.
684+
* @param statusCodePredicate to match responses with
685+
* @param exceptionFunction to map the response to an error signal
685686
* @return this builder
686687
* @since 5.1.9
687688
*/

0 commit comments

Comments
 (0)