Interface RestClient.RequestHeadersSpec<S extends RestClient.RequestHeadersSpec<S>>
- Type Parameters:
S
- a self reference to the spec type
- All Known Subinterfaces:
RestClient.RequestBodySpec, RestClient.RequestBodyUriSpec, RestClient.RequestHeadersUriSpec<S>
- Enclosing interface:
RestClient
- Since:
- 6.1
- Author:
- Arjen Poutsma, Sebastien Deleuze
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
Extension ofClientHttpResponse
that can convert the body.static interface
Defines the contract forexchange(ExchangeFunction)
.static interface
Variant ofRestClient.RequestHeadersSpec.ExchangeFunction
returning a non-null required value. -
Method Summary
Modifier and TypeMethodDescriptionSet the list of acceptable media types, as specified by theAccept
header.acceptCharset
(Charset... acceptableCharsets) Set the list of acceptable charsets, as specified by theAccept-Charset
header.apiVersion
(@Nullable Object version) Set an API version for the request.Set the attribute with the given name to the given value.attributes
(Consumer<Map<String, Object>> attributesConsumer) Provides access to every attribute declared so far with the possibility to add, replace, or remove values.Add a cookie with the given name and value.cookies
(Consumer<MultiValueMap<String, String>> cookiesConsumer) Provides access to every cookie declared so far with the possibility to add, replace, or remove values.exchange
(RestClient.RequestHeadersSpec.ExchangeFunction<T> exchangeFunction) Exchange theClientHttpResponse
for a value of typeT
.exchange
(RestClient.RequestHeadersSpec.ExchangeFunction<T> exchangeFunction, boolean close) Exchange theClientHttpResponse
for a value of typeT
.default <T> T
exchangeForRequiredValue
(RestClient.RequestHeadersSpec.RequiredValueExchangeFunction<T> exchangeFunction) Exchange theClientHttpResponse
for a value of typeT
.<T> T
exchangeForRequiredValue
(RestClient.RequestHeadersSpec.RequiredValueExchangeFunction<T> exchangeFunction, boolean close) Exchange theClientHttpResponse
for a value of typeT
.Add the given, single header value under the given name.headers
(Consumer<HttpHeaders> headersConsumer) Provides access to every header declared so far with the possibility to add, replace, or remove values.httpRequest
(Consumer<ClientHttpRequest> requestConsumer) Callback for access to theClientHttpRequest
that in turn provides access to the native request of the underlying HTTP library.ifModifiedSince
(ZonedDateTime ifModifiedSince) Set the value of theIf-Modified-Since
header.ifNoneMatch
(String... ifNoneMatches) Set the values of theIf-None-Match
header.retrieve()
Enter the retrieve workflow and use the returnedRestClient.ResponseSpec
to select from a number of built-in options to extract the response.
-
Method Details
-
accept
Set the list of acceptable media types, as specified by theAccept
header.- Parameters:
acceptableMediaTypes
- the acceptable media types- Returns:
- this builder
-
acceptCharset
-
cookie
-
cookies
Provides access to every cookie declared so far with the possibility to add, replace, or remove values.- Parameters:
cookiesConsumer
- the consumer to provide access to- Returns:
- this builder
- Since:
- 6.2
-
ifModifiedSince
Set the value of theIf-Modified-Since
header.- Parameters:
ifModifiedSince
- the new value of the header- Returns:
- this builder
-
ifNoneMatch
-
header
-
headers
Provides access to every header declared so far with the possibility to add, replace, or remove values.- Parameters:
headersConsumer
- the consumer to provide access to- Returns:
- this builder
-
apiVersion
Set an API version for the request. The version is inserted into the request through theconfigured
ApiVersionInserter
.If no version is set, the
defaultApiVersion
is used, if configured.If
null
is passed, then an API version is not inserted irrespective of default version settings.- Parameters:
version
- the API version for the request; this can be a String or some Object that can be formatted the inserter, e.g. through anApiVersionFormatter
.- Since:
- 7.0
-
attribute
-
attributes
-
httpRequest
Callback for access to theClientHttpRequest
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 are exposed by the underlying library.
- Parameters:
requestConsumer
- a consumer to access theClientHttpRequest
with- Returns:
- this builder
-
retrieve
Enter the retrieve workflow and use the returnedRestClient.ResponseSpec
to select from a number of built-in options to extract the response. For example:ResponseEntity<Person> entity = client.get() .uri("/persons/1") .accept(MediaType.APPLICATION_JSON) .retrieve() .toEntity(Person.class);
Or if interested only in the body:
Person person = client.get() .uri("/persons/1") .accept(MediaType.APPLICATION_JSON) .retrieve() .body(Person.class);
Note that this method does not actually execute the request until you call one of the returnedRestClient.ResponseSpec
. Use theexchange(ExchangeFunction)
variants if you need to separate request execution from response extraction.By default, 4xx response code result in a
HttpClientErrorException
and 5xx response codes in aHttpServerErrorException
. To customize error handling, useonStatus
handlers.- Returns:
ResponseSpec
to specify how to decode the body
-
exchange
default <T extends @Nullable Object> T exchange(RestClient.RequestHeadersSpec.ExchangeFunction<T> exchangeFunction) Exchange theClientHttpResponse
for a value of typeT
. This can be useful for advanced scenarios, for example to decode the response differently depending on the response status:Person person = client.get() .uri("/people/1") .accept(MediaType.APPLICATION_JSON) .exchange((request, response) -> { if (response.getStatusCode().equals(HttpStatus.OK)) { return deserialize(response.getBody()); } else { throw new BusinessException(); } });
Note: The response is closed after the exchange function has been invoked.
- Type Parameters:
T
- the type the response will be transformed to- Parameters:
exchangeFunction
- the function to handle the response with- Returns:
- the value returned from the exchange function, potentially
null
- See Also:
-
exchangeForRequiredValue
default <T> T exchangeForRequiredValue(RestClient.RequestHeadersSpec.RequiredValueExchangeFunction<T> exchangeFunction) Exchange theClientHttpResponse
for a value of typeT
. This can be useful for advanced scenarios, for example to decode the response differently depending on the response status:Person person = client.get() .uri("/people/1") .accept(MediaType.APPLICATION_JSON) .exchange((request, response) -> { if (response.getStatusCode().equals(HttpStatus.OK)) { return deserialize(response.getBody()); } else { throw new BusinessException(); } });
Note: The response is closed after the exchange function has been invoked.
- Type Parameters:
T
- the type the response will be transformed to- Parameters:
exchangeFunction
- the function to handle the response with- Returns:
- the value returned from the exchange function, never
null
- Since:
- 6.2.6
-
exchange
<T extends @Nullable Object> T exchange(RestClient.RequestHeadersSpec.ExchangeFunction<T> exchangeFunction, boolean close) Exchange theClientHttpResponse
for a value of typeT
. This can be useful for advanced scenarios, for example to decode the response differently depending on the response status:Person person = client.get() .uri("/people/1") .accept(MediaType.APPLICATION_JSON) .exchange((request, response) -> { if (response.getStatusCode().equals(HttpStatus.OK)) { return deserialize(response.getBody()); } else { throw new BusinessException(); } });
Note: If
close
istrue
, then the response is closed after the exchange function has been invoked. When set tofalse
, the caller is responsible for closing the response.- Type Parameters:
T
- the type the response will be transformed to- Parameters:
exchangeFunction
- the function to handle the response withclose
-true
to close the response afterexchangeFunction
is invoked,false
to keep it open- Returns:
- the value returned from the exchange function, potentially
null
- See Also:
-
exchangeForRequiredValue
<T> T exchangeForRequiredValue(RestClient.RequestHeadersSpec.RequiredValueExchangeFunction<T> exchangeFunction, boolean close) Exchange theClientHttpResponse
for a value of typeT
. This can be useful for advanced scenarios, for example to decode the response differently depending on the response status:Person person = client.get() .uri("/people/1") .accept(MediaType.APPLICATION_JSON) .exchange((request, response) -> { if (response.getStatusCode().equals(HttpStatus.OK)) { return deserialize(response.getBody()); } else { throw new BusinessException(); } });
Note: If
close
istrue
, then the response is closed after the exchange function has been invoked. When set tofalse
, the caller is responsible for closing the response.- Type Parameters:
T
- the type the response will be transformed to- Parameters:
exchangeFunction
- the function to handle the response withclose
-true
to close the response afterexchangeFunction
is invoked,false
to keep it open- Returns:
- the value returned from the exchange function, never
null
- Since:
- 6.2.6
-