Class RestTemplateBuilder
java.lang.Object
org.springframework.boot.web.client.RestTemplateBuilder
@ImportRuntimeHints(org.springframework.boot.web.client.RestTemplateBuilder.RestTemplateBuilderRuntimeHints.class)
public class RestTemplateBuilder
extends Object
Builder that can be used to configure and create a
RestTemplate. Provides
convenience methods to register converters, error handlers and
UriTemplateHandlers.
By default, the built RestTemplate will attempt to use the most suitable
ClientHttpRequestFactory, call detectRequestFactory(false) if you prefer to keep the default. In a typical
auto-configured Spring Boot application this builder is available as a bean and can be
injected whenever a RestTemplate is needed.
- Since:
- 1.4.0
- Author:
- Stephane Nicoll, Phillip Webb, Andy Wilkinson, Brian Clozel, Dmytro Nosan, Kevin Strijbos, Ilya Lukyanovich
-
Constructor Summary
ConstructorsConstructorDescriptionRestTemplateBuilder(RestTemplateCustomizer... customizers) Create a newRestTemplateBuilderinstance. -
Method Summary
Modifier and TypeMethodDescriptionadditionalCustomizers(Collection<? extends RestTemplateCustomizer> customizers) AddRestTemplateCustomizersthat should be applied to theRestTemplate.additionalCustomizers(RestTemplateCustomizer... customizers) AddRestTemplateCustomizersthat should be applied to theRestTemplate.additionalInterceptors(Collection<? extends ClientHttpRequestInterceptor> interceptors) Add additionalClientHttpRequestInterceptorsthat should be used with theRestTemplate.additionalInterceptors(ClientHttpRequestInterceptor... interceptors) Add additionalClientHttpRequestInterceptorsthat should be used with theRestTemplate.additionalMessageConverters(Collection<? extends HttpMessageConverter<?>> messageConverters) Add additionalHttpMessageConvertersthat should be used with theRestTemplate.additionalMessageConverters(HttpMessageConverter<?>... messageConverters) Add additionalHttpMessageConvertersthat should be used with theRestTemplate.additionalRequestCustomizers(Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers) Add theRestTemplateRequestCustomizersthat should be applied to theClientHttpRequest.additionalRequestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers) Add theRestTemplateRequestCustomizersthat should be applied to theClientHttpRequest.basicAuthentication(String username, String password) Add HTTP Basic Authentication to requests with the given username/password pair, unless a custom Authorization header has been set before.basicAuthentication(String username, String password, Charset charset) Add HTTP Basic Authentication to requests with the given username/password pair, unless a custom Authorization header has been set before.build()Build a newRestTemplateinstance and configure it using this builder.<T extends RestTemplate>
TBuild a newRestTemplateinstance of the specified type and configure it using this builder.Build a newClientHttpRequestFactoryinstance using the settings of this builder.<T extends RestTemplate>
Tconfigure(T restTemplate) Configure the providedRestTemplateinstance using this builder.customizers(Collection<? extends RestTemplateCustomizer> customizers) Set theRestTemplateCustomizersthat should be applied to theRestTemplate.customizers(RestTemplateCustomizer... customizers) Set theRestTemplateCustomizersthat should be applied to theRestTemplate.defaultHeader(String name, String... values) Add a default header that will be set if not already present on the outgoingHttpClientRequest.Set theHttpMessageConvertersthat should be used with theRestTemplateto the default set.detectRequestFactory(boolean detectRequestFactory) Set if theClientHttpRequestFactoryshould be detected based on the classpath.errorHandler(ResponseErrorHandler errorHandler) Set theResponseErrorHandlerthat should be used with theRestTemplate.interceptors(Collection<ClientHttpRequestInterceptor> interceptors) Set theClientHttpRequestInterceptorsthat should be used with theRestTemplate.interceptors(ClientHttpRequestInterceptor... interceptors) Set theClientHttpRequestInterceptorsthat should be used with theRestTemplate.messageConverters(Collection<? extends HttpMessageConverter<?>> messageConverters) Set theHttpMessageConvertersthat should be used with theRestTemplate.messageConverters(HttpMessageConverter<?>... messageConverters) Set theHttpMessageConvertersthat should be used with theRestTemplate.requestCustomizers(Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers) Set theRestTemplateRequestCustomizersthat should be applied to theClientHttpRequest.requestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers) Set theRestTemplateRequestCustomizersthat should be applied to theClientHttpRequest.requestFactory(Class<? extends ClientHttpRequestFactory> requestFactory) Set theClientHttpRequestFactoryclass that should be used with theRestTemplate.requestFactory(Supplier<ClientHttpRequestFactory> requestFactory) Set theSupplierofClientHttpRequestFactorythat should be called each time webuild()a newRestTemplateinstance.Set a root URL that should be applied to each request that starts with'/'.setBufferRequestBody(boolean bufferRequestBody) Sets if the underlyingClientHttpRequestFactoryshould buffer the request body internally.setConnectTimeout(Duration connectTimeout) Sets the connection timeout on the underlyingClientHttpRequestFactory.setReadTimeout(Duration readTimeout) Sets the read timeout on the underlyingClientHttpRequestFactory.uriTemplateHandler(UriTemplateHandler uriTemplateHandler) Set theUriTemplateHandlerthat should be used with theRestTemplate.
-
Constructor Details
-
RestTemplateBuilder
Create a newRestTemplateBuilderinstance.- Parameters:
customizers- anyRestTemplateCustomizersthat should be applied when theRestTemplateis built
-
-
Method Details
-
detectRequestFactory
Set if theClientHttpRequestFactoryshould be detected based on the classpath. Default iftrue.- Parameters:
detectRequestFactory- if theClientHttpRequestFactoryshould be detected- Returns:
- a new builder instance
-
rootUri
Set a root URL that should be applied to each request that starts with'/'. Since this works by adding aUriTemplateHandlerto theRestTemplate, the root URL will only apply whenStringvariants of theRestTemplatemethods are used for specifying the request URL. SeeRootUriTemplateHandlerfor details.- Parameters:
rootUri- the root URI ornull- Returns:
- a new builder instance
-
messageConverters
Set theHttpMessageConvertersthat should be used with theRestTemplate. Setting this value will replace any previously configured converters and any converters configured on the builder will replace RestTemplate's default converters.- Parameters:
messageConverters- the converters to set- Returns:
- a new builder instance
- See Also:
-
messageConverters
public RestTemplateBuilder messageConverters(Collection<? extends HttpMessageConverter<?>> messageConverters) Set theHttpMessageConvertersthat should be used with theRestTemplate. Setting this value will replace any previously configured converters and any converters configured on the builder will replace RestTemplate's default converters.- Parameters:
messageConverters- the converters to set- Returns:
- a new builder instance
- See Also:
-
additionalMessageConverters
public RestTemplateBuilder additionalMessageConverters(HttpMessageConverter<?>... messageConverters) Add additionalHttpMessageConvertersthat should be used with theRestTemplate. Any converters configured on the builder will replace RestTemplate's default converters.- Parameters:
messageConverters- the converters to add- Returns:
- a new builder instance
- See Also:
-
additionalMessageConverters
public RestTemplateBuilder additionalMessageConverters(Collection<? extends HttpMessageConverter<?>> messageConverters) Add additionalHttpMessageConvertersthat should be used with theRestTemplate. Any converters configured on the builder will replace RestTemplate's default converters.- Parameters:
messageConverters- the converters to add- Returns:
- a new builder instance
- See Also:
-
defaultMessageConverters
Set theHttpMessageConvertersthat should be used with theRestTemplateto the default set. Calling this method will replace any previously defined converters.- Returns:
- a new builder instance
- See Also:
-
interceptors
Set theClientHttpRequestInterceptorsthat should be used with theRestTemplate. Setting this value will replace any previously defined interceptors.- Parameters:
interceptors- the interceptors to set- Returns:
- a new builder instance
- Since:
- 1.4.1
- See Also:
-
interceptors
Set theClientHttpRequestInterceptorsthat should be used with theRestTemplate. Setting this value will replace any previously defined interceptors.- Parameters:
interceptors- the interceptors to set- Returns:
- a new builder instance
- Since:
- 1.4.1
- See Also:
-
additionalInterceptors
Add additionalClientHttpRequestInterceptorsthat should be used with theRestTemplate.- Parameters:
interceptors- the interceptors to add- Returns:
- a new builder instance
- Since:
- 1.4.1
- See Also:
-
additionalInterceptors
public RestTemplateBuilder additionalInterceptors(Collection<? extends ClientHttpRequestInterceptor> interceptors) Add additionalClientHttpRequestInterceptorsthat should be used with theRestTemplate.- Parameters:
interceptors- the interceptors to add- Returns:
- a new builder instance
- Since:
- 1.4.1
- See Also:
-
requestFactory
Set theClientHttpRequestFactoryclass that should be used with theRestTemplate.- Parameters:
requestFactory- the request factory to use- Returns:
- a new builder instance
-
requestFactory
Set theSupplierofClientHttpRequestFactorythat should be called each time webuild()a newRestTemplateinstance.- Parameters:
requestFactory- the supplier for the request factory- Returns:
- a new builder instance
- Since:
- 2.0.0
-
uriTemplateHandler
Set theUriTemplateHandlerthat should be used with theRestTemplate.- Parameters:
uriTemplateHandler- the URI template handler to use- Returns:
- a new builder instance
-
errorHandler
Set theResponseErrorHandlerthat should be used with theRestTemplate.- Parameters:
errorHandler- the error handler to use- Returns:
- a new builder instance
-
basicAuthentication
Add HTTP Basic Authentication to requests with the given username/password pair, unless a custom Authorization header has been set before.- Parameters:
username- the user namepassword- the password- Returns:
- a new builder instance
- Since:
- 2.1.0
- See Also:
-
basicAuthentication
Add HTTP Basic Authentication to requests with the given username/password pair, unless a custom Authorization header has been set before.- Parameters:
username- the user namepassword- the passwordcharset- the charset to use- Returns:
- a new builder instance
- Since:
- 2.2.0
-
defaultHeader
Add a default header that will be set if not already present on the outgoingHttpClientRequest.- Parameters:
name- the name of the headervalues- the header values- Returns:
- a new builder instance
- Since:
- 2.2.0
-
setConnectTimeout
Sets the connection timeout on the underlyingClientHttpRequestFactory.- Parameters:
connectTimeout- the connection timeout- Returns:
- a new builder instance.
- Since:
- 2.1.0
-
setReadTimeout
Sets the read timeout on the underlyingClientHttpRequestFactory.- Parameters:
readTimeout- the read timeout- Returns:
- a new builder instance.
- Since:
- 2.1.0
-
setBufferRequestBody
Sets if the underlyingClientHttpRequestFactoryshould buffer the request body internally.- Parameters:
bufferRequestBody- value of the bufferRequestBody parameter- Returns:
- a new builder instance.
- Since:
- 2.2.0
- See Also:
-
customizers
Set theRestTemplateCustomizersthat should be applied to theRestTemplate. Customizers are applied in the order that they were added after builder configuration has been applied. Setting this value will replace any previously configured customizers.- Parameters:
customizers- the customizers to set- Returns:
- a new builder instance
- See Also:
-
customizers
Set theRestTemplateCustomizersthat should be applied to theRestTemplate. Customizers are applied in the order that they were added after builder configuration has been applied. Setting this value will replace any previously configured customizers.- Parameters:
customizers- the customizers to set- Returns:
- a new builder instance
- See Also:
-
additionalCustomizers
AddRestTemplateCustomizersthat should be applied to theRestTemplate. Customizers are applied in the order that they were added after builder configuration has been applied.- Parameters:
customizers- the customizers to add- Returns:
- a new builder instance
- See Also:
-
additionalCustomizers
public RestTemplateBuilder additionalCustomizers(Collection<? extends RestTemplateCustomizer> customizers) AddRestTemplateCustomizersthat should be applied to theRestTemplate. Customizers are applied in the order that they were added after builder configuration has been applied.- Parameters:
customizers- the customizers to add- Returns:
- a new builder instance
- See Also:
-
requestCustomizers
public RestTemplateBuilder requestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers) Set theRestTemplateRequestCustomizersthat should be applied to theClientHttpRequest. Customizers are applied in the order that they were added. Setting this value will replace any previously configured request customizers.- Parameters:
requestCustomizers- the request customizers to set- Returns:
- a new builder instance
- Since:
- 2.2.0
- See Also:
-
requestCustomizers
public RestTemplateBuilder requestCustomizers(Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers) Set theRestTemplateRequestCustomizersthat should be applied to theClientHttpRequest. Customizers are applied in the order that they were added. Setting this value will replace any previously configured request customizers.- Parameters:
requestCustomizers- the request customizers to set- Returns:
- a new builder instance
- Since:
- 2.2.0
- See Also:
-
additionalRequestCustomizers
public RestTemplateBuilder additionalRequestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers) Add theRestTemplateRequestCustomizersthat should be applied to theClientHttpRequest. Customizers are applied in the order that they were added.- Parameters:
requestCustomizers- the request customizers to add- Returns:
- a new builder instance
- Since:
- 2.2.0
- See Also:
-
additionalRequestCustomizers
public RestTemplateBuilder additionalRequestCustomizers(Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers) Add theRestTemplateRequestCustomizersthat should be applied to theClientHttpRequest. Customizers are applied in the order that they were added.- Parameters:
requestCustomizers- the request customizers to add- Returns:
- a new builder instance
- Since:
- 2.2.0
- See Also:
-
build
Build a newRestTemplateinstance and configure it using this builder.- Returns:
- a configured
RestTemplateinstance. - See Also:
-
build
Build a newRestTemplateinstance of the specified type and configure it using this builder.- Type Parameters:
T- the type of rest template- Parameters:
restTemplateClass- the template type to create- Returns:
- a configured
RestTemplateinstance. - See Also:
-
configure
Configure the providedRestTemplateinstance using this builder.- Type Parameters:
T- the type of rest template- Parameters:
restTemplate- theRestTemplateto configure- Returns:
- the rest template instance
- See Also:
-
buildRequestFactory
Build a newClientHttpRequestFactoryinstance using the settings of this builder.- Returns:
- a
ClientHttpRequestFactoryornull - Since:
- 2.2.0
-