Package org.springframework.web.client
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
public static interface RestClient.RequestHeadersSpec<S extends RestClient.RequestHeadersSpec<S>>
Contract for specifying request headers leading up to the exchange.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceExtension ofClientHttpResponsethat can convert the body.static interfaceDefines the contract forexchange(ExchangeFunction). -
Method Summary
Modifier and TypeMethodDescriptionSet the list of acceptable media types, as specified by theAcceptheader.acceptCharset(Charset... acceptableCharsets) Set the list of acceptable charsets, as specified by theAccept-Charsetheader.default <T> Texchange(RestClient.RequestHeadersSpec.ExchangeFunction<T> exchangeFunction) Exchange theClientHttpResponsefor a typeT.<T> Texchange(RestClient.RequestHeadersSpec.ExchangeFunction<T> exchangeFunction, boolean close) Exchange theClientHttpResponsefor a 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 theClientHttpRequestthat in turn provides access to the native request of the underlying HTTP library.ifModifiedSince(ZonedDateTime ifModifiedSince) Set the value of theIf-Modified-Sinceheader.ifNoneMatch(String... ifNoneMatches) Set the values of theIf-None-Matchheader.retrieve()Proceed to declare how to extract the response.
-
Method Details
-
accept
Set the list of acceptable media types, as specified by theAcceptheader.- Parameters:
acceptableMediaTypes- the acceptable media types- Returns:
- this builder
-
acceptCharset
Set the list of acceptable charsets, as specified by theAccept-Charsetheader.- Parameters:
acceptableCharsets- the acceptable charsets- Returns:
- this builder
-
ifModifiedSince
Set the value of theIf-Modified-Sinceheader.- Parameters:
ifModifiedSince- the new value of the header- Returns:
- this builder
-
ifNoneMatch
Set the values of theIf-None-Matchheader.- Parameters:
ifNoneMatches- the new value of the header- Returns:
- this builder
-
header
Add the given, single header value under the given name.- Parameters:
headerName- the header nameheaderValues- the header value(s)- Returns:
- this builder
-
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
-
httpRequest
Callback for access to theClientHttpRequestthat 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 theClientHttpRequestwith- Returns:
- this builder
-
retrieve
RestClient.ResponseSpec retrieve()Proceed to declare how to extract the response. For example to extract aResponseEntitywith status, headers, and body: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);By default, 4xx response code result in a
HttpClientErrorExceptionand 5xx response codes in aHttpServerErrorException. To customize error handling, useonStatushandlers.- Returns:
ResponseSpecto specify how to decode the body
-
exchange
@Nullable default <T> T exchange(RestClient.RequestHeadersSpec.ExchangeFunction<T> exchangeFunction) Exchange theClientHttpResponsefor a 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
-
exchange
@Nullable <T> T exchange(RestClient.RequestHeadersSpec.ExchangeFunction<T> exchangeFunction, boolean close) Exchange theClientHttpResponsefor a 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
closeistrue, 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-trueto close the response afterexchangeFunctionis invoked,falseto keep it open- Returns:
- the value returned from the exchange function
-