Interface RestClient.Builder
- Enclosing interface:
- RestClient
RestClient
.-
Method Summary
Modifier and TypeMethodDescriptionapply
(Consumer<RestClient.Builder> builderConsumer) Apply the givenConsumer
to this builder instance.Configure a base URL for requests.build()
Build theRestClient
instance.clone()
Clone thisRestClient.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.defaultHeaders
(Consumer<HttpHeaders> headersConsumer) Provide a consumer to access to every default header declared so far, with the possibility to add, replace, or remove.defaultRequest
(Consumer<RestClient.RequestHeadersSpec<?>> defaultRequest) Provide a consumer to customize every request being built.defaultStatusHandler
(Predicate<HttpStatusCode> statusPredicate, RestClient.ResponseSpec.ErrorHandler errorHandler) Register a default status handler to apply to every response.defaultStatusHandler
(ResponseErrorHandler errorHandler) Register a default status handler to apply to every response.defaultUriVariables
(Map<String, ?> defaultUriVariables) Configure default URL variable values to use when expanding URI templates with aMap
.messageConverters
(Consumer<List<HttpMessageConverter<?>>> configurer) Configure the message converters for theRestClient
to use.observationConvention
(ClientRequestObservationConvention observationConvention) Configure theObservationConvention
to use for collecting metadata for the request observation.observationRegistry
(io.micrometer.observation.ObservationRegistry observationRegistry) Configure theObservationRegistry
to use for recording HTTP client observations.requestFactory
(ClientHttpRequestFactory requestFactory) Configure theClientHttpRequestFactory
to use.requestInitializer
(ClientHttpRequestInitializer initializer) Add the given request initializer to the end of the initializer chain.requestInitializers
(Consumer<List<ClientHttpRequestInitializer>> initializersConsumer) Manipulate the initializers with the given consumer.requestInterceptor
(ClientHttpRequestInterceptor interceptor) Add the given request interceptor to the end of the interceptor chain.requestInterceptors
(Consumer<List<ClientHttpRequestInterceptor>> interceptorsConsumer) Manipulate the interceptors with the given consumer.uriBuilderFactory
(UriBuilderFactory uriBuilderFactory) Provide a pre-configuredUriBuilderFactory
instance.
-
Method Details
-
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, thebaseUrl
value provided here will be ignored.- Returns:
- this builder
- See Also:
-
defaultUriVariables
Configure default URL variable values to use when expanding URI templates with aMap
. 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, thedefaultUriVariables
value provided here will be ignored.- Returns:
- this builder
- See Also:
-
uriBuilderFactory
Provide a pre-configuredUriBuilderFactory
instance. This is an alternative to, and effectively overrides the following shortcut properties:- Parameters:
uriBuilderFactory
- the URI builder factory to use- Returns:
- this builder
- See Also:
-
defaultHeader
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 namevalues
- the header values- Returns:
- this builder
-
defaultHeaders
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
-
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 witherrorHandler
- handler that typically, though not necessarily, throws an exception- Returns:
- this builder
-
defaultStatusHandler
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.The first status handler who claims that a response has an error is invoked. If you want to disable other defaults, consider using
defaultStatusHandler(Predicate, ResponseSpec.ErrorHandler)
with a predicate that matches all status codes.- Parameters:
errorHandler
- handler that typically, though not necessarily, throws an exception- Returns:
- this builder
-
requestInterceptor
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
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
Configure theClientHttpRequestFactory
to use. This is useful for plugging in and/or customizing options of the underlying HTTP client library (e.g. 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 thejava.net.http
module is loaded, or to a simple default otherwise.- Parameters:
requestFactory
- the request factory to use- Returns:
- this builder
-
messageConverters
Configure the message converters for theRestClient
to use.- Parameters:
configurer
- the configurer to apply- Returns:
- this builder
-
observationRegistry
RestClient.Builder observationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry) Configure theObservationRegistry
to use for recording HTTP client observations.- Parameters:
observationRegistry
- the observation registry to use- Returns:
- this builder
-
observationConvention
Configure theObservationConvention
to use for collecting metadata for the request observation. Will useDefaultClientRequestObservationConvention
if none provided.- Parameters:
observationConvention
- the observation convention to use- Returns:
- this builder
-
apply
Apply the givenConsumer
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 thisRestClient.Builder
. -
build
RestClient build()Build theRestClient
instance.
-