S
- a self reference to the spec typepublic static interface WebClient.RequestHeadersSpec<S extends WebClient.RequestHeadersSpec<S>>
Modifier and Type | Method and Description |
---|---|
S |
accept(MediaType... acceptableMediaTypes)
Set the list of acceptable media types, as
specified by the
Accept header. |
S |
acceptCharset(Charset... acceptableCharsets)
Set the list of acceptable charsets, as specified
by the
Accept-Charset header. |
S |
attribute(String name,
Object value)
Set the attribute with the given name to the given value.
|
S |
attributes(Consumer<Map<String,Object>> attributesConsumer)
Provides access to every attribute declared so far with the
possibility to add, replace, or remove values.
|
S |
context(Function<reactor.util.context.Context,reactor.util.context.Context> contextModifier)
Deprecated.
in 5.3.2 to be removed soon after; this method cannot
provide context to downstream (nested or subsequent) requests and is
of limited value.
|
S |
cookie(String name,
String value)
Add a cookie with the given name and value.
|
S |
cookies(Consumer<MultiValueMap<String,String>> cookiesConsumer)
Provides access to every cookie declared so far with the possibility
to add, replace, or remove values.
|
reactor.core.publisher.Mono<ClientResponse> |
exchange()
Deprecated.
since 5.3 due to the possibility to leak memory and/or
connections; please, use
exchangeToMono(Function) ,
exchangeToFlux(Function) ; consider also using
retrieve() which provides access to the response status
and headers via ResponseEntity along with error status
handling. |
<V> reactor.core.publisher.Flux<V> |
exchangeToFlux(Function<ClientResponse,? extends reactor.core.publisher.Flux<V>> responseHandler)
An alternative to
retrieve() that provides more control via
access to the ClientResponse . |
<V> reactor.core.publisher.Mono<V> |
exchangeToMono(Function<ClientResponse,? extends reactor.core.publisher.Mono<V>> responseHandler)
An alternative to
retrieve() that provides more control via
access to the ClientResponse . |
S |
header(String headerName,
String... headerValues)
Add the given, single header value under the given name.
|
S |
headers(Consumer<HttpHeaders> headersConsumer)
Provides access to every header declared so far with the possibility
to add, replace, or remove values.
|
S |
httpRequest(Consumer<ClientHttpRequest> requestConsumer)
Callback for access to the
ClientHttpRequest that in turn
provides access to the native request of the underlying HTTP library. |
S |
ifModifiedSince(ZonedDateTime ifModifiedSince)
Set the value of the
If-Modified-Since header. |
S |
ifNoneMatch(String... ifNoneMatches)
Set the values of the
If-None-Match header. |
WebClient.ResponseSpec |
retrieve()
Proceed to declare how to extract the response.
|
S accept(MediaType... acceptableMediaTypes)
Accept
header.acceptableMediaTypes
- the acceptable media typesS acceptCharset(Charset... acceptableCharsets)
Accept-Charset
header.acceptableCharsets
- the acceptable charsetsS cookie(String name, String value)
name
- the cookie namevalue
- the cookie valueS cookies(Consumer<MultiValueMap<String,String>> cookiesConsumer)
cookiesConsumer
- the consumer to provide access toS ifModifiedSince(ZonedDateTime ifModifiedSince)
If-Modified-Since
header.
The date should be specified as the number of milliseconds since January 1, 1970 GMT.
ifModifiedSince
- the new value of the headerS ifNoneMatch(String... ifNoneMatches)
If-None-Match
header.ifNoneMatches
- the new value of the headerS header(String headerName, String... headerValues)
headerName
- the header nameheaderValues
- the header value(s)S headers(Consumer<HttpHeaders> headersConsumer)
headersConsumer
- the consumer to provide access toS attribute(String name, Object value)
name
- the name of the attribute to addvalue
- the value of the attribute to addS attributes(Consumer<Map<String,Object>> attributesConsumer)
attributesConsumer
- the consumer to provide access to@Deprecated S context(Function<reactor.util.context.Context,reactor.util.context.Context> contextModifier)
Context
.contextModifier
- the function to modify the context withS httpRequest(Consumer<ClientHttpRequest> requestConsumer)
ClientHttpRequest
that in turn
provides access to the native request of the underlying HTTP library.
This could be useful for setting advanced, per-request options that
exposed by the underlying library.requestConsumer
- a consumer to access the
ClientHttpRequest
withResponseSpec
to specify how to decode the bodyWebClient.ResponseSpec retrieve()
ResponseEntity
with status, headers, and body:
Mono<ResponseEntity<Person>> entityMono = client.get() .uri("/persons/1") .accept(MediaType.APPLICATION_JSON) .retrieve() .toEntity(Person.class);
Or if interested only in the body:
Mono<Person> entityMono = client.get() .uri("/persons/1") .accept(MediaType.APPLICATION_JSON) .retrieve() .bodyToMono(Person.class);
By default, 4xx and 5xx responses result in a
WebClientResponseException
. To customize error handling, use
onStatus
handlers.
<V> reactor.core.publisher.Mono<V> exchangeToMono(Function<ClientResponse,? extends reactor.core.publisher.Mono<V>> responseHandler)
retrieve()
that provides more control via
access to the ClientResponse
. This can be useful for advanced
scenarios, for example to decode the response differently depending
on the response status:
Mono<Person> entityMono = client.get() .uri("/persons/1") .accept(MediaType.APPLICATION_JSON) .exchangeToMono(response -> { if (response.statusCode().equals(HttpStatus.OK)) { return response.bodyToMono(Person.class); } else { return response.createException().flatMap(Mono::error); } });
Note: After the returned Mono
completes,
the response body is automatically released if it hasn't been consumed.
If the response content is needed, the provided function must declare
how to decode it.
V
- the type of Object the response will be transformed toresponseHandler
- the function to handle the response withMono
produced from the response<V> reactor.core.publisher.Flux<V> exchangeToFlux(Function<ClientResponse,? extends reactor.core.publisher.Flux<V>> responseHandler)
retrieve()
that provides more control via
access to the ClientResponse
. This can be useful for advanced
scenarios, for example to decode the response differently depending
on the response status:
Flux<Person> entityMono = client.get() .uri("/persons") .accept(MediaType.APPLICATION_JSON) .exchangeToFlux(response -> { if (response.statusCode().equals(HttpStatus.OK)) { return response.bodyToFlux(Person.class); } else { return response.createException().flatMapMany(Mono::error); } });
Note: After the returned Flux
completes,
the response body is automatically released if it hasn't been consumed.
If the response content is needed, the provided function must declare
how to decode it.
V
- the type of Objects the response will be transformed toresponseHandler
- the function to handle the response withFlux
of Objects produced from the response@Deprecated reactor.core.publisher.Mono<ClientResponse> exchange()
exchangeToMono(Function)
,
exchangeToFlux(Function)
; consider also using
retrieve()
which provides access to the response status
and headers via ResponseEntity
along with error status
handling.ClientResponse
with the
response status and headers. You can then use methods of the response
to consume the body:
Mono<Person> mono = client.get() .uri("/persons/1") .accept(MediaType.APPLICATION_JSON) .exchange() .flatMap(response -> response.bodyToMono(Person.class)); Flux<Person> flux = client.get() .uri("/persons") .accept(MediaType.APPLICATION_STREAM_JSON) .exchange() .flatMapMany(response -> response.bodyToFlux(Person.class));
NOTE: Unlike retrieve()
, when using
exchange()
, it is the responsibility of the application to
consume any response content regardless of the scenario (success,
error, unexpected data, etc). Not doing so can cause a memory leak.
See ClientResponse
for a list of all the available options
for consuming the body. Generally prefer using retrieve()
unless you have a good reason to use exchange()
which does
allow to check the response status and headers before deciding how or
if to consume the response.
Mono
for the responseretrieve()