Class RestTemplateBuilder
java.lang.Object
org.springframework.boot.web.client.RestTemplateBuilder
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
-
Constructor Summary
ConstructorDescriptionRestTemplateBuilder
(RestTemplateCustomizer... customizers) Create a newRestTemplateBuilder
instance. -
Method Summary
Modifier and TypeMethodDescriptionadditionalCustomizers
(Collection<? extends RestTemplateCustomizer> customizers) AddRestTemplateCustomizers
that should be applied to theRestTemplate
.additionalCustomizers
(RestTemplateCustomizer... customizers) AddRestTemplateCustomizers
that should be applied to theRestTemplate
.additionalInterceptors
(Collection<? extends org.springframework.http.client.ClientHttpRequestInterceptor> interceptors) Add additionalClientHttpRequestInterceptors
that should be used with theRestTemplate
.additionalInterceptors
(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors) Add additionalClientHttpRequestInterceptors
that should be used with theRestTemplate
.additionalMessageConverters
(Collection<? extends org.springframework.http.converter.HttpMessageConverter<?>> messageConverters) Add additionalHttpMessageConverters
that should be used with theRestTemplate
.additionalMessageConverters
(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters) Add additionalHttpMessageConverters
that should be used with theRestTemplate
.additionalRequestCustomizers
(Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers) Add theRestTemplateRequestCustomizers
that should be applied to theClientHttpRequest
.additionalRequestCustomizers
(RestTemplateRequestCustomizer<?>... requestCustomizers) Add theRestTemplateRequestCustomizers
that 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.org.springframework.web.client.RestTemplate
build()
Build a newRestTemplate
instance and configure it using this builder.<T extends org.springframework.web.client.RestTemplate>
TBuild a newRestTemplate
instance of the specified type and configure it using this builder.org.springframework.http.client.ClientHttpRequestFactory
Build a newClientHttpRequestFactory
instance using the settings of this builder.<T extends org.springframework.web.client.RestTemplate>
Tconfigure
(T restTemplate) Configure the providedRestTemplate
instance using this builder.connectTimeout
(Duration connectTimeout) Sets the connection timeout on the underlyingClientHttpRequestFactory
.customizers
(Collection<? extends RestTemplateCustomizer> customizers) Set theRestTemplateCustomizers
that should be applied to theRestTemplate
.customizers
(RestTemplateCustomizer... customizers) Set theRestTemplateCustomizers
that should be applied to theRestTemplate
.defaultHeader
(String name, String... values) Add a default header that will be set if not already present on the outgoingClientHttpRequest
.Set theHttpMessageConverters
that should be used with theRestTemplate
to the default set.detectRequestFactory
(boolean detectRequestFactory) Set if theClientHttpRequestFactory
should be detected based on the classpath.errorHandler
(org.springframework.web.client.ResponseErrorHandler errorHandler) Set theResponseErrorHandler
that should be used with theRestTemplate
.interceptors
(Collection<org.springframework.http.client.ClientHttpRequestInterceptor> interceptors) Set theClientHttpRequestInterceptors
that should be used with theRestTemplate
.interceptors
(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors) Set theClientHttpRequestInterceptors
that should be used with theRestTemplate
.messageConverters
(Collection<? extends org.springframework.http.converter.HttpMessageConverter<?>> messageConverters) Set theHttpMessageConverters
that should be used with theRestTemplate
.messageConverters
(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters) Set theHttpMessageConverters
that should be used with theRestTemplate
.readTimeout
(Duration readTimeout) Sets the read timeout on the underlyingClientHttpRequestFactory
.Sets the redirect strategy on the underlyingClientHttpRequestFactory
.requestCustomizers
(Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers) Set theRestTemplateRequestCustomizers
that should be applied to theClientHttpRequest
.requestCustomizers
(RestTemplateRequestCustomizer<?>... requestCustomizers) Set theRestTemplateRequestCustomizers
that should be applied to theClientHttpRequest
.requestFactory
(Class<? extends org.springframework.http.client.ClientHttpRequestFactory> requestFactoryType) Set theClientHttpRequestFactory
class that should be used with theRestTemplate
.requestFactory
(Function<ClientHttpRequestFactorySettings, org.springframework.http.client.ClientHttpRequestFactory> requestFactoryFunction) Deprecated, for removal: This API element is subject to removal in a future version.requestFactory
(Supplier<org.springframework.http.client.ClientHttpRequestFactory> requestFactorySupplier) Set theSupplier
ofClientHttpRequestFactory
that should be called each time webuild()
a newRestTemplate
instance.requestFactoryBuilder
(ClientHttpRequestFactoryBuilder<?> requestFactoryBuilder) Set theClientHttpRequestFactoryBuilder
that should be used each time webuild()
a newRestTemplate
instance.requestFactorySettings
(UnaryOperator<ClientHttpRequestFactorySettings> requestFactorySettingsCustomizer) Update theClientHttpRequestFactorySettings
using the given customizer.requestFactorySettings
(ClientHttpRequestFactorySettings requestFactorySettings) Sets theClientHttpRequestFactorySettings
.Set a root URL that should be applied to each request that starts with'/'
.setConnectTimeout
(Duration connectTimeout) Deprecated, for removal: This API element is subject to removal in a future version.since 3.4.0 for removal in 3.6.0 in favor ofconnectTimeout(Duration)
setReadTimeout
(Duration readTimeout) Deprecated, for removal: This API element is subject to removal in a future version.since 3.4.0 for removal in 3.6.0 in favor ofreadTimeout(Duration)
setSslBundle
(SslBundle sslBundle) Deprecated, for removal: This API element is subject to removal in a future version.since 3.4.0 for removal in 3.6.0 in favor ofsslBundle(SslBundle)
Sets the SSL bundle on the underlyingClientHttpRequestFactory
.uriTemplateHandler
(org.springframework.web.util.UriTemplateHandler uriTemplateHandler) Set theUriTemplateHandler
that should be used with theRestTemplate
.
-
Constructor Details
-
RestTemplateBuilder
Create a newRestTemplateBuilder
instance.- Parameters:
customizers
- anyRestTemplateCustomizers
that should be applied when theRestTemplate
is built
-
-
Method Details
-
detectRequestFactory
Set if theClientHttpRequestFactory
should be detected based on the classpath. Default iftrue
.- Parameters:
detectRequestFactory
- if theClientHttpRequestFactory
should be detected- Returns:
- a new builder instance
-
rootUri
Set a root URL that should be applied to each request that starts with'/'
. The root URL will only apply whenString
variants of theRestTemplate
methods are used for specifying the request URL.- Parameters:
rootUri
- the root URI ornull
- Returns:
- a new builder instance
-
messageConverters
public RestTemplateBuilder messageConverters(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters) Set theHttpMessageConverters
that 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 org.springframework.http.converter.HttpMessageConverter<?>> messageConverters) Set theHttpMessageConverters
that 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(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters) Add additionalHttpMessageConverters
that 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 org.springframework.http.converter.HttpMessageConverter<?>> messageConverters) Add additionalHttpMessageConverters
that 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 theHttpMessageConverters
that should be used with theRestTemplate
to the default set. Calling this method will replace any previously defined converters.- Returns:
- a new builder instance
- See Also:
-
interceptors
public RestTemplateBuilder interceptors(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors) Set theClientHttpRequestInterceptors
that 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
public RestTemplateBuilder interceptors(Collection<org.springframework.http.client.ClientHttpRequestInterceptor> interceptors) Set theClientHttpRequestInterceptors
that 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
public RestTemplateBuilder additionalInterceptors(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors) Add additionalClientHttpRequestInterceptors
that 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 org.springframework.http.client.ClientHttpRequestInterceptor> interceptors) Add additionalClientHttpRequestInterceptors
that should be used with theRestTemplate
.- Parameters:
interceptors
- the interceptors to add- Returns:
- a new builder instance
- Since:
- 1.4.1
- See Also:
-
requestFactory
public RestTemplateBuilder requestFactory(Class<? extends org.springframework.http.client.ClientHttpRequestFactory> requestFactoryType) Set theClientHttpRequestFactory
class that should be used with theRestTemplate
.- Parameters:
requestFactoryType
- the request factory type to use- Returns:
- a new builder instance
- See Also:
-
requestFactory
public RestTemplateBuilder requestFactory(Supplier<org.springframework.http.client.ClientHttpRequestFactory> requestFactorySupplier) Set theSupplier
ofClientHttpRequestFactory
that should be called each time webuild()
a newRestTemplate
instance.- Parameters:
requestFactorySupplier
- the supplier for the request factory- Returns:
- a new builder instance
- Since:
- 2.0.0
- See Also:
-
requestFactory
@Deprecated(since="3.4.0", forRemoval=true) public RestTemplateBuilder requestFactory(Function<ClientHttpRequestFactorySettings, org.springframework.http.client.ClientHttpRequestFactory> requestFactoryFunction) Deprecated, for removal: This API element is subject to removal in a future version.since 3.4.0 for removal in 3.6.0 in favor ofrequestFactoryBuilder(ClientHttpRequestFactoryBuilder)
Set the request factory function that should be called to provide aClientHttpRequestFactory
each time webuild()
a newRestTemplate
instance.- Parameters:
requestFactoryFunction
- the settings to request factory function- Returns:
- a new builder instance
- Since:
- 3.0.0
- See Also:
-
requestFactoryBuilder
public RestTemplateBuilder requestFactoryBuilder(ClientHttpRequestFactoryBuilder<?> requestFactoryBuilder) Set theClientHttpRequestFactoryBuilder
that should be used each time webuild()
a newRestTemplate
instance.- Parameters:
requestFactoryBuilder
- theClientHttpRequestFactoryBuilder
to use- Returns:
- a new builder instance
- Since:
- 3.4.0
- See Also:
-
uriTemplateHandler
public RestTemplateBuilder uriTemplateHandler(org.springframework.web.util.UriTemplateHandler uriTemplateHandler) Set theUriTemplateHandler
that should be used with theRestTemplate
.- Parameters:
uriTemplateHandler
- the URI template handler to use- Returns:
- a new builder instance
-
errorHandler
public RestTemplateBuilder errorHandler(org.springframework.web.client.ResponseErrorHandler errorHandler) Set theResponseErrorHandler
that 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 outgoingClientHttpRequest
.- Parameters:
name
- the name of the headervalues
- the header values- Returns:
- a new builder instance
- Since:
- 2.2.0
-
requestFactorySettings
public RestTemplateBuilder requestFactorySettings(ClientHttpRequestFactorySettings requestFactorySettings) Sets theClientHttpRequestFactorySettings
. This will replace any previously setconnectTimeout
,readTimeout
andsslBundle
values.- Parameters:
requestFactorySettings
- the request factory settings- Returns:
- a new builder instance
- Since:
- 3.4.0
-
requestFactorySettings
public RestTemplateBuilder requestFactorySettings(UnaryOperator<ClientHttpRequestFactorySettings> requestFactorySettingsCustomizer) Update theClientHttpRequestFactorySettings
using the given customizer.- Parameters:
requestFactorySettingsCustomizer
- aUnaryOperator
to update request factory settings- Returns:
- a new builder instance
- Since:
- 3.4.1
-
setConnectTimeout
@Deprecated(since="3.4.0", forRemoval=true) public RestTemplateBuilder setConnectTimeout(Duration connectTimeout) Deprecated, for removal: This API element is subject to removal in a future version.since 3.4.0 for removal in 3.6.0 in favor ofconnectTimeout(Duration)
Sets the connection timeout on the underlyingClientHttpRequestFactory
.- Parameters:
connectTimeout
- the connection timeout- Returns:
- a new builder instance.
- Since:
- 2.1.0
-
connectTimeout
Sets the connection timeout on the underlyingClientHttpRequestFactory
.- Parameters:
connectTimeout
- the connection timeout- Returns:
- a new builder instance.
- Since:
- 3.4.0
-
setReadTimeout
@Deprecated(since="3.4.0", forRemoval=true) public RestTemplateBuilder setReadTimeout(Duration readTimeout) Deprecated, for removal: This API element is subject to removal in a future version.since 3.4.0 for removal in 3.6.0 in favor ofreadTimeout(Duration)
Sets the read timeout on the underlyingClientHttpRequestFactory
.- Parameters:
readTimeout
- the read timeout- Returns:
- a new builder instance.
- Since:
- 2.1.0
-
readTimeout
Sets the read timeout on the underlyingClientHttpRequestFactory
.- Parameters:
readTimeout
- the read timeout- Returns:
- a new builder instance.
- Since:
- 3.4.0
-
redirects
Sets the redirect strategy on the underlyingClientHttpRequestFactory
.- Parameters:
redirects
- the redirect strategy- Returns:
- a new builder instance.
- Since:
- 3.4.1
-
setSslBundle
@Deprecated(since="3.4.0", forRemoval=true) public RestTemplateBuilder setSslBundle(SslBundle sslBundle) Deprecated, for removal: This API element is subject to removal in a future version.since 3.4.0 for removal in 3.6.0 in favor ofsslBundle(SslBundle)
Sets the SSL bundle on the underlyingClientHttpRequestFactory
.- Parameters:
sslBundle
- the SSL bundle- Returns:
- a new builder instance
- Since:
- 3.1.0
-
sslBundle
Sets the SSL bundle on the underlyingClientHttpRequestFactory
.- Parameters:
sslBundle
- the SSL bundle- Returns:
- a new builder instance
- Since:
- 3.4.0
-
customizers
Set theRestTemplateCustomizers
that 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 theRestTemplateCustomizers
that 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
AddRestTemplateCustomizers
that 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) AddRestTemplateCustomizers
that 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 theRestTemplateRequestCustomizers
that 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 theRestTemplateRequestCustomizers
that 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 theRestTemplateRequestCustomizers
that 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 theRestTemplateRequestCustomizers
that 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
public org.springframework.web.client.RestTemplate build()Build a newRestTemplate
instance and configure it using this builder.- Returns:
- a configured
RestTemplate
instance. - See Also:
-
build
Build a newRestTemplate
instance 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
RestTemplate
instance. - See Also:
-
configure
public <T extends org.springframework.web.client.RestTemplate> T configure(T restTemplate) Configure the providedRestTemplate
instance using this builder.- Type Parameters:
T
- the type of rest template- Parameters:
restTemplate
- theRestTemplate
to configure- Returns:
- the rest template instance
- See Also:
-
buildRequestFactory
public org.springframework.http.client.ClientHttpRequestFactory buildRequestFactory()Build a newClientHttpRequestFactory
instance using the settings of this builder.- Returns:
- a
ClientHttpRequestFactory
ornull
- Since:
- 2.2.0
-
requestFactoryBuilder(ClientHttpRequestFactoryBuilder)