Package org.springframework.web.client

# Interface RestClient.Builder

Enclosing interface:
  RestClient

public static interface RestClient.Builder
A mutable builder for creating a `RestClient`.

## Method Summary

Modifier and Type
Method
Description
`RestClient.Builder`
`apply(Consumer<RestClient.Builder> builderConsumer)`
Apply the given `Consumer` to this builder instance.
`RestClient.Builder`
`baseUrl(String baseUrl)`
Configure a base URL for requests.
`RestClient.Builder`
`baseUrl(URI baseUrl)`
Configure a base `URI` for requests.
`RestClient`
`build()`
Build the `RestClient` instance.
`RestClient.Builder`
`clone()`
Clone this `RestClient.Builder`.
`RestClient.Builder`
`defaultCookie(String cookie, String... values)`
Global option to specify a cookie to be added to every request, if the request does not already contain such a cookie.
`RestClient.Builder`
`defaultCookies(Consumer<org.springframework.util.MultiValueMap<String,String>> cookiesConsumer)`
Provides access to every `defaultCookie(String, String...)` declared so far with the possibility to add, replace, or remove.
`RestClient.Builder`
`defaultHeader(String header, String... values)`
Global option to specify a header to be added to every request, if the request does not already contain such a header.
`RestClient.Builder`
`defaultHeaders(Consumer<HttpHeaders> headersConsumer)`
Provide a consumer to access to every default header declared so far, with the possibility to add, replace, or remove.
`RestClient.Builder`
`defaultRequest(Consumer<RestClient.RequestHeadersSpec<?>> defaultRequest)`
Provide a consumer to customize every request being built.
`RestClient.Builder`
`defaultStatusHandler(Predicate<HttpStatusCode> statusPredicate, RestClient.ResponseSpec.ErrorHandler errorHandler)`
Register a default status handler to apply to every response.
`RestClient.Builder`
`defaultStatusHandler(ResponseErrorHandler errorHandler)`
Variant of `defaultStatusHandler(Predicate, ResponseSpec.ErrorHandler)` that allows use of `RestTemplate`'s `ResponseErrorHandler` mechanism.
`RestClient.Builder`
`defaultUriVariables(Map<String,?> defaultUriVariables)`
Configure default URL variable values to use when expanding URI templates with a `Map`.
`RestClient.Builder`
`messageConverters(Consumer<List<HttpMessageConverter<?>>> configurer)`
Configure the message converters for the `RestClient` to use.
`RestClient.Builder`
`messageConverters(List<HttpMessageConverter<?>> messageConverters)`
Set the message converters for the `RestClient` to use.
`RestClient.Builder`
`observationConvention(ClientRequestObservationConvention observationConvention)`
Configure the `ObservationConvention` to use for collecting metadata for the request observation.
`RestClient.Builder`
`observationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry)`
Configure the `ObservationRegistry` to use for recording HTTP client observations.
`RestClient.Builder`
`requestFactory(ClientHttpRequestFactory requestFactory)`
Configure the `ClientHttpRequestFactory` to use.
`RestClient.Builder`
`requestInitializer(ClientHttpRequestInitializer initializer)`
Add the given request initializer to the end of the initializer chain.
`RestClient.Builder`
`requestInitializers(Consumer<List<ClientHttpRequestInitializer>> initializersConsumer)`
Manipulate the initializers with the given consumer.
`RestClient.Builder`
`requestInterceptor(ClientHttpRequestInterceptor interceptor)`
Add the given request interceptor to the end of the interceptor chain.
`RestClient.Builder`
`requestInterceptors(Consumer<List<ClientHttpRequestInterceptor>> interceptorsConsumer)`
Manipulate the interceptors with the given consumer.
`RestClient.Builder`
`uriBuilderFactory(UriBuilderFactory uriBuilderFactory)`
Provide a pre-configured `UriBuilderFactory` instance.

## Method Details

### baseUrl

RestClient.Builder baseUrl(String baseUrl)
Configure a base URL for requests. Effectively a shortcut for:

     String baseUrl = "https://abc.go.com/v1";
     DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl);
     RestClient client = RestClient.builder().uriBuilderFactory(factory).build();

The `DefaultUriBuilderFactory` is used to prepare the URL for every request with the given base URL, unless the URL request for a given URL is absolute in which case the base URL is ignored.

Note: this method is mutually exclusive with `uriBuilderFactory(UriBuilderFactory)`. If both are used, the `baseUrl` value provided here will be ignored.

Returns:
  this builder
See Also:
  - `DefaultUriBuilderFactory(String)`
  - `uriBuilderFactory(UriBuilderFactory)`

### baseUrl

RestClient.Builder baseUrl(URI baseUrl)
Configure a base `URI` for requests. Effectively a shortcut for:

     URI baseUrl = URI.create("https://abc.go.com/v1");
     DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl.toString());
     RestClient client = RestClient.builder().uriBuilderFactory(factory).build();

The `DefaultUriBuilderFactory` is used to prepare the URL for every request with the given base URL, unless the URL request for a given URL is absolute in which case the base URL is ignored.

Note: this method is mutually exclusive with `uriBuilderFactory(UriBuilderFactory)`. If both are used, the `baseUrl` value provided here will be ignored.

Returns:
  this builder
Since:
  6.2
See Also:
  - `DefaultUriBuilderFactory(String)`
  - `uriBuilderFactory(UriBuilderFactory)`

### defaultUriVariables

RestClient.Builder defaultUriVariables(Map<String,?> defaultUriVariables)
Configure default URL variable values to use when expanding URI templates with a `Map`. Effectively a shortcut for:

     Map<String, ?> defaultVars = ...;
     DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory();
     factory.setDefaultVariables(defaultVars);
     RestClient client = RestClient.builder().uriBuilderFactory(factory).build();

Note: this method is mutually exclusive with `uriBuilderFactory(UriBuilderFactory)`. If both are used, the `defaultUriVariables` value provided here will be ignored.

Returns:
  this builder
See Also:
  - `DefaultUriBuilderFactory.setDefaultUriVariables(Map)`
  - `uriBuilderFactory(UriBuilderFactory)`

### uriBuilderFactory

RestClient.Builder uriBuilderFactory(UriBuilderFactory uriBuilderFactory)
Provide a pre-configured `UriBuilderFactory` instance. This is an alternative to, and effectively overrides the following shortcut properties:

- `baseUrl(String)`
- `defaultUriVariables(Map)`.

Parameters:
  `uriBuilderFactory` - the URI builder factory to use
Returns:
  this builder
See Also:
  - `baseUrl(String)`
  - `defaultUriVariables(Map)`

### defaultHeader

RestClient.Builder defaultHeader(String header, String... values)
Global option to specify a header to be added to every request, if the request does not already contain such a header.

Parameters:
  `header` - the header name
  `values` - the header values
Returns:
  this builder

### defaultHeaders

RestClient.Builder defaultHeaders(Consumer<HttpHeaders> headersConsumer)
Provide a consumer to access to every default header declared so far, with the possibility to add, replace, or remove.

Parameters:
  `headersConsumer` - the consumer
Returns:
  this builder

### defaultCookie

RestClient.Builder defaultCookie(String cookie, String... values)
Global option to specify a cookie to be added to every request, if the request does not already contain such a cookie.

Parameters:
  `cookie` - the cookie name
  `values` - the cookie values
Since:
  6.2

### defaultCookies

RestClient.Builder defaultCookies(Consumer<org.springframework.util.MultiValueMap<String,String>> cookiesConsumer)
Provides access to every `defaultCookie(String, String...)` declared so far with the possibility to add, replace, or remove.

Parameters:
  `cookiesConsumer` - a function that consumes the cookies map
Since:
  6.2

### defaultRequest

RestClient.Builder defaultRequest(Consumer<RestClient.RequestHeadersSpec<?>> defaultRequest)
Provide a consumer to customize every request being built.

Parameters:
  `defaultRequest` - the consumer to use for modifying requests
Returns:
  this builder

### defaultStatusHandler

RestClient.Builder defaultStatusHandler(Predicate<HttpStatusCode> statusPredicate, RestClient.ResponseSpec.ErrorHandler errorHandler)
Register a default status handler to apply to every response. Such default handlers are applied in the order in which they are registered, and after any others that are registered for a specific response.

Parameters:
  `statusPredicate` - to match responses with
  `errorHandler` - handler that typically, though not necessarily, throws an exception
Returns:
  this builder

### defaultStatusHandler

RestClient.Builder defaultStatusHandler(ResponseErrorHandler errorHandler)
Variant of `defaultStatusHandler(Predicate, ResponseSpec.ErrorHandler)` that allows use of `RestTemplate`'s `ResponseErrorHandler` mechanism. This is provided mainly to assist `RestTemplate` users to transition to `RestClient`. Internally, the given handler is adapted to `RestClient.ResponseSpec`, which is the preferred mechanism to use.

Parameters:
  `errorHandler` - the error handler to configure, internally adapted and integrated into the `RestClient.ResponseSpec.ErrorHandler` chain.
Returns:
  this builder

### requestInterceptor

RestClient.Builder requestInterceptor(ClientHttpRequestInterceptor interceptor)
Add the given request interceptor to the end of the interceptor chain.

Parameters:
  `interceptor` - the interceptor to be added to the chain
Returns:
  this builder

### requestInterceptors

RestClient.Builder requestInterceptors(Consumer<List<ClientHttpRequestInterceptor>> interceptorsConsumer)
Manipulate the interceptors with the given consumer. The list provided to the consumer is "live", so that the consumer can be used to remove interceptors, change ordering, etc.

Parameters:
  `interceptorsConsumer` - a function that consumes the interceptors list
Returns:
  this builder

### requestInitializer

RestClient.Builder requestInitializer(ClientHttpRequestInitializer initializer)
Add the given request initializer to the end of the initializer chain.

Parameters:
  `initializer` - the initializer to be added to the chain
Returns:
  this builder

### requestInitializers

RestClient.Builder requestInitializers(Consumer<List<ClientHttpRequestInitializer>> initializersConsumer)
Manipulate the initializers with the given consumer. The list provided to the consumer is "live", so that the consumer can be used to remove initializers, change ordering, etc.

Parameters:
  `initializersConsumer` - a function that consumes the initializers list
Returns:
  this builder

### requestFactory

RestClient.Builder requestFactory(ClientHttpRequestFactory requestFactory)
Configure the `ClientHttpRequestFactory` to use. This is useful for plugging in and/or customizing options of the underlying HTTP client library (for example, SSL).

If no request factory is specified, `RestClient` uses Apache Http Client, Jetty Http Client if available on the classpath, and defaults to the JDK HttpClient if the `java.net.http` module is loaded, or to a simple default otherwise.

Parameters:
  `requestFactory` - the request factory to use
Returns:
  this builder

### messageConverters

RestClient.Builder messageConverters(Consumer<List<HttpMessageConverter<?>>> configurer)
Configure the message converters for the `RestClient` to use.

Parameters:
  `configurer` - the configurer to apply on the list of default `HttpMessageConverter` pre-initialized
Returns:
  this builder
See Also:
  - `messageConverters(List)`

### messageConverters

RestClient.Builder messageConverters(List<HttpMessageConverter<?>> messageConverters)
Set the message converters for the `RestClient` to use.

Parameters:
  `messageConverters` - the list of `HttpMessageConverter` to use
Returns:
  this builder
Since:
  6.2
See Also:
  - `messageConverters(Consumer)`

### observationRegistry

RestClient.Builder observationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry)
Configure the `ObservationRegistry` to use for recording HTTP client observations.

Parameters:
  `observationRegistry` - the observation registry to use
Returns:
  this builder

### observationConvention

RestClient.Builder observationConvention(ClientRequestObservationConvention observationConvention)
Configure the `ObservationConvention` to use for collecting metadata for the request observation. Will use `DefaultClientRequestObservationConvention` if none provided.

Parameters:
  `observationConvention` - the observation convention to use
Returns:
  this builder

### apply

RestClient.Builder apply(Consumer<RestClient.Builder> builderConsumer)
Apply the given `Consumer` to this builder instance.

This can be useful for applying pre-packaged customizations.

Parameters:
  `builderConsumer` - the consumer to apply
Returns:
  this builder

### clone

RestClient.Builder clone()
Clone this `RestClient.Builder`.

### build

RestClient build()
Build the `RestClient` instance.