Interface RestClient.Builder
- Enclosing interface:
RestClient
RestClient.- Since:
- 6.1
- Author:
- Arjen Poutsma, Sebastien Deleuze
-
Method Summary
Modifier and TypeMethodDescriptionapiVersionInserter(@Nullable ApiVersionInserter apiVersionInserter) Configure anApiVersionInserterto abstract how an API version specified viaRestClient.RequestHeadersSpec.apiVersion(Object)is inserted into the request.apply(Consumer<RestClient.Builder> builderConsumer) Apply the givenConsumerto this builder instance.Configure a base URL for requests.Configure a baseURIfor requests.bufferContent(BiPredicate<URI, HttpMethod> predicate) Enable buffering of request and response, aggregating all content before it is sent, and making it possible to read the response body repeatedly.build()Build theRestClientinstance.clone()Clone thisRestClient.Builder.Configure the message converters for theRestClientto use.defaultApiVersion(Object version) Global option to specify an API version to be added to every request, if not explicitly set.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.defaultCookies(Consumer<MultiValueMap<String, String>> cookiesConsumer) Provides access to everydefaultCookie(String, String...)declared so far with the possibility to add, replace, or remove.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(Iterable<HttpMessageConverter<?>> messageConverters) Deprecated, for removal: This API element is subject to removal in a future version.messageConverters(Consumer<List<HttpMessageConverter<?>>> configurer) Deprecated, for removal: This API element is subject to removal in a future version.since 7.0 in favor ofconfigureMessageConverters(Consumer)observationConvention(ClientRequestObservationConvention observationConvention) Configure theObservationConventionto use for collecting metadata for the request observation.observationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry) Configure theObservationRegistryto use for recording HTTP client observations.requestFactory(ClientHttpRequestFactory requestFactory) Configure theClientHttpRequestFactoryto 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-configuredUriBuilderFactoryinstance.
-
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
DefaultUriBuilderFactoryis 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, thebaseUrlvalue provided here will be ignored.- Returns:
- this builder
- See Also:
-
baseUrl
Configure a baseURIfor 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
DefaultUriBuilderFactoryis 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, thebaseUrlvalue provided here will be ignored.- Returns:
- this builder
- Since:
- 6.2
- 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, thedefaultUriVariablesvalue provided here will be ignored.- Returns:
- this builder
- See Also:
-
uriBuilderFactory
Provide a pre-configuredUriBuilderFactoryinstance. 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
-
defaultCookie
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 namevalues- the cookie values- Since:
- 6.2
-
defaultCookies
Provides access to everydefaultCookie(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
-
defaultApiVersion
Global option to specify an API version to be added to every request, if not explicitly set.- Parameters:
version- the version to use- Returns:
- this builder
- Since:
- 7.0
-
apiVersionInserter
Configure anApiVersionInserterto abstract how an API version specified viaRestClient.RequestHeadersSpec.apiVersion(Object)is inserted into the request.ApiVersionInserterexposes shortcut methods for several built-in inserter implementation types. See the class-level Javadoc ofApiVersionInserterfor a list of choices.- Parameters:
apiVersionInserter- the inserter to use- Since:
- 7.0
-
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
-
bufferContent
Enable buffering of request and response, aggregating all content before it is sent, and making it possible to read the response body repeatedly.- Parameters:
predicate- to determine whether to buffer for the given request- Returns:
- this builder
- Since:
- 7.0
-
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 theClientHttpRequestFactoryto 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,
RestClientuses Apache Http Client, Jetty Http Client if available on the classpath, and defaults to the JDK HttpClient if thejava.net.httpmodule is loaded, or to a simple default otherwise.- Parameters:
requestFactory- the request factory to use- Returns:
- this builder
-
configureMessageConverters
RestClient.Builder configureMessageConverters(Consumer<HttpMessageConverters.ClientBuilder> configurer) Configure the message converters for theRestClientto use. Multiple consumers are composed together and applied to a singleHttpMessageConverters.ClientBuilderinstance.- Parameters:
configurer- the configurer to apply on an emptyHttpMessageConverters.ClientBuilder.- Returns:
- this builder
- Since:
- 7.0
-
messageConverters
@Deprecated(since="7.0", forRemoval=true) RestClient.Builder messageConverters(Iterable<HttpMessageConverter<?>> messageConverters) Deprecated, for removal: This API element is subject to removal in a future version.since 7.0 in favor ofconfigureMessageConverters(Consumer)Set the message converters to use.Note: As of 7.0, the converters provided here populate a
HttpMessageConverters.ClientBuilderinitially, and after that the same builder is initialized further through the configurers provided viaconfigureMessageConverters(Consumer).- Parameters:
messageConverters- the converters to use- Returns:
- this builder
- Since:
- 6.2
-
messageConverters
@Deprecated(since="7.0", forRemoval=true) RestClient.Builder messageConverters(Consumer<List<HttpMessageConverter<?>>> configurer) Deprecated, for removal: This API element is subject to removal in a future version.since 7.0 in favor ofconfigureMessageConverters(Consumer)Customize the message converters to use, which is either the default converters, or those provided viamessageConverters(Iterable). The consumer is applied immediately to the internal listNote: As of 7.0, the list of converters customized here is used to populate a
HttpMessageConverters.ClientBuilderinitially, and after that the same builder is initialized further through the configurers provided viaconfigureMessageConverters(Consumer).- Parameters:
configurer- the configurer to apply on the list of defaultHttpMessageConverterpre-initialized- Returns:
- this builder
-
observationRegistry
RestClient.Builder observationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry) Configure theObservationRegistryto use for recording HTTP client observations.- Parameters:
observationRegistry- the observation registry to use- Returns:
- this builder
-
observationConvention
Configure theObservationConventionto use for collecting metadata for the request observation. Will useDefaultClientRequestObservationConventionif none provided.- Parameters:
observationConvention- the observation convention to use- Returns:
- this builder
-
apply
Apply the givenConsumerto 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 theRestClientinstance.
-
configureMessageConverters(Consumer)