Package org.springframework.web.client

# Interface RestClient

public interface RestClient
Client to perform HTTP requests, exposing a fluent, synchronous API over underlying HTTP client libraries such as the JDK `HttpClient`, Apache HttpComponents, and others.

Use static factory methods `create()`, `create(String)`, or `builder()` to prepare an instance. To use the same configuration as a `RestTemplate`, use `create(RestTemplate)` or `builder(RestTemplate)`.

For examples with a response body see:

- `retrieve()`
- `exchange(Function<ClientHttpRequest, T>)`

For examples with a request body see:

- `body(Object)`
- `body(Object, ParameterizedTypeReference)`
- `body(Consumer<OutputStream>)`

Since:
  6.1
Author:
  Arjen Poutsma, Sebastien Deleuze

## Nested Class Summary

Nested Classes
Modifier and Type
Interface
Description
`static interface`
`RestClient.Builder`
A mutable builder for creating a `RestClient`.
`static interface`
`RestClient.RequestBodySpec`
Contract for specifying request headers and body leading up to the exchange.
`static interface`
`RestClient.RequestBodyUriSpec`
Contract for specifying request headers, body and URI for a request.
`static interface`
`RestClient.RequestHeadersSpec<S extends RestClient.RequestHeadersSpec<S>>`
Contract for specifying request headers leading up to the exchange.
`static interface`
`RestClient.RequestHeadersUriSpec<S extends RestClient.RequestHeadersSpec<S>>`
Contract for specifying request headers and URI for a request.
`static interface`
`RestClient.ResponseSpec`
Contract for specifying response operations following the exchange.
`static interface`
`RestClient.UriSpec<S extends RestClient.RequestHeadersSpec<?>>`
Contract for specifying the URI for a request.

## Method Summary

Modifier and Type
Method
Description
`static RestClient.Builder`
`builder()`
Obtain a `RestClient` builder.
`static RestClient.Builder`
`builder(RestTemplate restTemplate)`
Obtain a `RestClient` builder based on the configuration of the given `RestTemplate`.
`static RestClient`
`create()`
Create a new `RestClient`.
`static RestClient`
`create(String baseUrl)`
Variant of `create()` that accepts a default base URL.
`static RestClient`
`create(URI baseUrl)`
Variant of `create()` that accepts a default base `URI`.
`static RestClient`
`create(RestTemplate restTemplate)`
Create a new `RestClient` based on the configuration of the given `RestTemplate`.
`RestClient.RequestHeadersUriSpec<?>`
`delete()`
Start building an HTTP DELETE request.
`RestClient.RequestHeadersUriSpec<?>`
`get()`
Start building an HTTP GET request.
`RestClient.RequestHeadersUriSpec<?>`
`head()`
Start building an HTTP HEAD request.
`RestClient.RequestBodyUriSpec`
`method(HttpMethod method)`
Start building a request for the given `HttpMethod`.
`RestClient.Builder`
`mutate()`
Return a builder to create a new `RestClient` whose settings are replicated from this `RestClient`.
`RestClient.RequestHeadersUriSpec<?>`
`options()`
Start building an HTTP OPTIONS request.
`RestClient.RequestBodyUriSpec`
`patch()`
Start building an HTTP PATCH request.
`RestClient.RequestBodyUriSpec`
`post()`
Start building an HTTP POST request.
`RestClient.RequestBodyUriSpec`
`put()`
Start building an HTTP PUT request.

## Method Details

### get

RestClient.RequestHeadersUriSpec<?> get()
Start building an HTTP GET request.

Returns:
  a spec for specifying the target URL

### head

RestClient.RequestHeadersUriSpec<?> head()
Start building an HTTP HEAD request.

Returns:
  a spec for specifying the target URL

### post

RestClient.RequestBodyUriSpec post()
Start building an HTTP POST request.

Returns:
  a spec for specifying the target URL

### put

RestClient.RequestBodyUriSpec put()
Start building an HTTP PUT request.

Returns:
  a spec for specifying the target URL

### patch

RestClient.RequestBodyUriSpec patch()
Start building an HTTP PATCH request.

Returns:
  a spec for specifying the target URL

### delete

RestClient.RequestHeadersUriSpec<?> delete()
Start building an HTTP DELETE request.

Returns:
  a spec for specifying the target URL

### options

RestClient.RequestHeadersUriSpec<?> options()
Start building an HTTP OPTIONS request.

Returns:
  a spec for specifying the target URL

### method

RestClient.RequestBodyUriSpec method(HttpMethod method)
Start building a request for the given `HttpMethod`.

Returns:
  a spec for specifying the target URL

### mutate

RestClient.Builder mutate()
Return a builder to create a new `RestClient` whose settings are replicated from this `RestClient`.

### create

static RestClient create()
Create a new `RestClient`.

See Also:
  - `create(String)`
  - `builder()`

### create

static RestClient create(String baseUrl)
Variant of `create()` that accepts a default base URL. For more details see `Builder.baseUrl(String)`.

Parameters:
  `baseUrl` - the base URI for all requests
See Also:
  - `builder()`

### create

static RestClient create(URI baseUrl)
Variant of `create()` that accepts a default base `URI`. For more details see `Builder.baseUrl(URI)`.

Parameters:
  `baseUrl` - the base URI for all requests
Since:
  6.2
See Also:
  - `builder()`

### create

static RestClient create(RestTemplate restTemplate)
Create a new `RestClient` based on the configuration of the given `RestTemplate`.

The returned builder is configured with the following attributes of the template.

- `ClientHttpRequestFactory`
- `HttpMessageConverters`
- `ClientHttpRequestInterceptors`
- `ClientHttpRequestInitializers`
- `UriBuilderFactory`
- error handler

Parameters:
  `restTemplate` - the rest template to base the returned client's configuration on
Returns:
  a `RestClient` initialized with the `restTemplate`'s configuration

### builder

static RestClient.Builder builder()
Obtain a `RestClient` builder.

### builder

static RestClient.Builder builder(RestTemplate restTemplate)
Obtain a `RestClient` builder based on the configuration of the given `RestTemplate`.

The returned builder is configured with the following attributes of the template.

- `ClientHttpRequestFactory`
- `HttpMessageConverters`
- `ClientHttpRequestInterceptors`
- `ClientHttpRequestInitializers`
- `UriBuilderFactory`
- error handler

Parameters:
  `restTemplate` - the rest template to base the returned builder's configuration on
Returns:
  a `RestClient` builder initialized with `restTemplate`'s configuration