Interface WebClient.ResponseSpec
- Enclosing interface:
- WebClient
public static interface WebClient.ResponseSpec
Contract for specifying response operations following the exchange.
-
Method Summary
Modifier and TypeMethodDescription<T> reactor.core.publisher.Flux<T>
bodyToFlux
(Class<T> elementClass) Decode the body to aFlux
with elements of the given type.<T> reactor.core.publisher.Flux<T>
bodyToFlux
(ParameterizedTypeReference<T> elementTypeRef) Variant ofbodyToMono(Class)
with aParameterizedTypeReference
.<T> reactor.core.publisher.Mono<T>
bodyToMono
(Class<T> elementClass) Decode the body to the given target type.<T> reactor.core.publisher.Mono<T>
bodyToMono
(ParameterizedTypeReference<T> elementTypeRef) Variant ofbodyToMono(Class)
with aParameterizedTypeReference
.onRawStatus
(IntPredicate statusCodePredicate, Function<ClientResponse, reactor.core.publisher.Mono<? extends Throwable>> exceptionFunction) Variant ofonStatus(Predicate, Function)
that works with raw status code values.onStatus
(Predicate<HttpStatus> statusPredicate, Function<ClientResponse, reactor.core.publisher.Mono<? extends Throwable>> exceptionFunction) Provide a function to map specific error status codes to an error signal to be propagated downstream instead of the response.reactor.core.publisher.Mono<ResponseEntity<Void>>
Return aResponseEntity
without a body.<T> reactor.core.publisher.Mono<ResponseEntity<T>>
Return aResponseEntity
with the body decoded to an Object of the given type.<T> reactor.core.publisher.Mono<ResponseEntity<T>>
toEntity
(ParameterizedTypeReference<T> bodyTypeReference) Variant ofbodyToMono(Class)
with aParameterizedTypeReference
.<T> reactor.core.publisher.Mono<ResponseEntity<reactor.core.publisher.Flux<T>>>
toEntityFlux
(Class<T> elementType) Return aResponseEntity
with the body decoded to aFlux
of elements of the given type.<T> reactor.core.publisher.Mono<ResponseEntity<reactor.core.publisher.Flux<T>>>
toEntityFlux
(ParameterizedTypeReference<T> elementTypeReference) Variant oftoEntityFlux(Class)
with aParameterizedTypeReference
.<T> reactor.core.publisher.Mono<ResponseEntity<reactor.core.publisher.Flux<T>>>
toEntityFlux
(BodyExtractor<reactor.core.publisher.Flux<T>, ? super ClientHttpResponse> bodyExtractor) Variant oftoEntityFlux(Class)
with aBodyExtractor
.<T> reactor.core.publisher.Mono<ResponseEntity<List<T>>>
toEntityList
(Class<T> elementClass) Return aResponseEntity
with the body decoded to aList
of elements of the given type.<T> reactor.core.publisher.Mono<ResponseEntity<List<T>>>
toEntityList
(ParameterizedTypeReference<T> elementTypeRef) Variant oftoEntity(Class)
with aParameterizedTypeReference
.
-
Method Details
-
onStatus
WebClient.ResponseSpec onStatus(Predicate<HttpStatus> statusPredicate, Function<ClientResponse, reactor.core.publisher.Mono<? extends Throwable>> exceptionFunction) Provide a function to map specific error status codes to an error signal to be propagated downstream instead of the response.By default, if there are no matching status handlers, responses with status codes >= 400 are mapped to
WebClientResponseException
which is created withClientResponse.createException()
.To suppress the treatment of a status code as an error and process it as a normal response, return
Mono.empty()
from the function. The response will then propagate downstream to be processed.To ignore an error response completely, and propagate neither response nor error, use a
filter
, or addonErrorResume
downstream, for example:webClient.get() .uri("https://abc.com/account/123") .retrieve() .bodyToMono(Account.class) .onErrorResume(WebClientResponseException.class, ex -> ex.getRawStatusCode() == 404 ? Mono.empty() : Mono.error(ex));
- Parameters:
statusPredicate
- to match responses withexceptionFunction
- to map the response to an error signal- Returns:
- this builder
- See Also:
-
onRawStatus
WebClient.ResponseSpec onRawStatus(IntPredicate statusCodePredicate, Function<ClientResponse, reactor.core.publisher.Mono<? extends Throwable>> exceptionFunction) Variant ofonStatus(Predicate, Function)
that works with raw status code values. This is useful for custom status codes.- Parameters:
statusCodePredicate
- to match responses withexceptionFunction
- to map the response to an error signal- Returns:
- this builder
- Since:
- 5.1.9
-
bodyToMono
Decode the body to the given target type. For an error response (status code of 4xx or 5xx), theMono
emits aWebClientException
. UseonStatus(Predicate, Function)
to customize error response handling.- Type Parameters:
T
- the target body type- Parameters:
elementClass
- the type to decode to- Returns:
- the decoded body
-
bodyToMono
Variant ofbodyToMono(Class)
with aParameterizedTypeReference
.- Type Parameters:
T
- the target body type- Parameters:
elementTypeRef
- the type to decode to- Returns:
- the decoded body
-
bodyToFlux
Decode the body to aFlux
with elements of the given type. For an error response (status code of 4xx or 5xx), theMono
emits aWebClientException
. UseonStatus(Predicate, Function)
to customize error response handling.- Type Parameters:
T
- the body element type- Parameters:
elementClass
- the type of element to decode to- Returns:
- the decoded body
-
bodyToFlux
Variant ofbodyToMono(Class)
with aParameterizedTypeReference
.- Type Parameters:
T
- the body element type- Parameters:
elementTypeRef
- the type of element to decode to- Returns:
- the decoded body
-
toEntity
Return aResponseEntity
with the body decoded to an Object of the given type. For an error response (status code of 4xx or 5xx), theMono
emits aWebClientException
. UseonStatus(Predicate, Function)
to customize error response handling.- Type Parameters:
T
- response body type- Parameters:
bodyClass
- the expected response body type- Returns:
- the
ResponseEntity
with the decoded body - Since:
- 5.2
-
toEntity
<T> reactor.core.publisher.Mono<ResponseEntity<T>> toEntity(ParameterizedTypeReference<T> bodyTypeReference) Variant ofbodyToMono(Class)
with aParameterizedTypeReference
.- Type Parameters:
T
- the response body type- Parameters:
bodyTypeReference
- the expected response body type- Returns:
- the
ResponseEntity
with the decoded body - Since:
- 5.2
-
toEntityList
Return aResponseEntity
with the body decoded to aList
of elements of the given type. For an error response (status code of 4xx or 5xx), theMono
emits aWebClientException
. UseonStatus(Predicate, Function)
to customize error response handling.- Type Parameters:
T
- the body element type- Parameters:
elementClass
- the type of element to decode the target Flux to- Returns:
- the
ResponseEntity
- Since:
- 5.2
-
toEntityList
<T> reactor.core.publisher.Mono<ResponseEntity<List<T>>> toEntityList(ParameterizedTypeReference<T> elementTypeRef) Variant oftoEntity(Class)
with aParameterizedTypeReference
.- Type Parameters:
T
- the body element type- Parameters:
elementTypeRef
- the type of element to decode the target Flux to- Returns:
- the
ResponseEntity
- Since:
- 5.2
-
toEntityFlux
<T> reactor.core.publisher.Mono<ResponseEntity<reactor.core.publisher.Flux<T>>> toEntityFlux(Class<T> elementType) Return aResponseEntity
with the body decoded to aFlux
of elements of the given type. For an error response (status code of 4xx or 5xx), theMono
emits aWebClientException
. UseonStatus(Predicate, Function)
to customize error response handling.Note: The
Flux
representing the body must be subscribed to or else associated resources will not be released.- Type Parameters:
T
- the body element type- Parameters:
elementType
- the type of element to decode the target Flux to- Returns:
- the
ResponseEntity
- Since:
- 5.3.1
-
toEntityFlux
<T> reactor.core.publisher.Mono<ResponseEntity<reactor.core.publisher.Flux<T>>> toEntityFlux(ParameterizedTypeReference<T> elementTypeReference) Variant oftoEntityFlux(Class)
with aParameterizedTypeReference
.- Type Parameters:
T
- the body element type- Parameters:
elementTypeReference
- the type of element to decode the target Flux to- Returns:
- the
ResponseEntity
- Since:
- 5.3.1
-
toEntityFlux
<T> reactor.core.publisher.Mono<ResponseEntity<reactor.core.publisher.Flux<T>>> toEntityFlux(BodyExtractor<reactor.core.publisher.Flux<T>, ? super ClientHttpResponse> bodyExtractor) Variant oftoEntityFlux(Class)
with aBodyExtractor
.- Type Parameters:
T
- the body element type- Parameters:
bodyExtractor
- theBodyExtractor
that reads from the response- Returns:
- the
ResponseEntity
- Since:
- 5.3.2
-
toBodilessEntity
reactor.core.publisher.Mono<ResponseEntity<Void>> toBodilessEntity()Return aResponseEntity
without a body. For an error response (status code of 4xx or 5xx), theMono
emits aWebClientException
. UseonStatus(Predicate, Function)
to customize error response handling.- Returns:
- the
ResponseEntity
- Since:
- 5.2
-