Class RestTemplateBuilder
- java.lang.Object
- org.springframework.boot.web.client.RestTemplateBuilder
public class RestTemplateBuilder extends Object
Builder that can be used to configure and create aRestTemplate
. Provides convenience methods to registerconverters
,error handlers
andUriTemplateHandlers
.By default the built
RestTemplate
will attempt to use the most suitableClientHttpRequestFactory
, calldetectRequestFactory(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 aRestTemplate
is needed.- Since:
- 1.4.0
Constructor Summary
Constructors Constructor Description RestTemplateBuilder(RestTemplateCustomizer... customizers)
Create a newRestTemplateBuilder
instance.
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description RestTemplateBuilder
additionalCustomizers(Collection<? extends RestTemplateCustomizer> customizers)
AddRestTemplateCustomizers
that should be applied to theRestTemplate
.RestTemplateBuilder
additionalCustomizers(RestTemplateCustomizer... restTemplateCustomizers)
AddRestTemplateCustomizers
that should be applied to theRestTemplate
.RestTemplateBuilder
additionalInterceptors(Collection<? extends org.springframework.http.client.ClientHttpRequestInterceptor> interceptors)
Add additionalClientHttpRequestInterceptors
that should be used with theRestTemplate
.RestTemplateBuilder
additionalInterceptors(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors)
Add additionalClientHttpRequestInterceptors
that should be used with theRestTemplate
.RestTemplateBuilder
additionalMessageConverters(Collection<? extends org.springframework.http.converter.HttpMessageConverter<?>> messageConverters)
Add additionalHttpMessageConverters
that should be used with theRestTemplate
.RestTemplateBuilder
additionalMessageConverters(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters)
Add additionalHttpMessageConverters
that should be used with theRestTemplate
.RestTemplateBuilder
basicAuthentication(String username, String password)
Add HTTP basic authentication to requests.RestTemplateBuilder
basicAuthorization(String username, String password)
Deprecated.since 2.1.0 in favor ofbasicAuthentication(String username, String password)
org.springframework.web.client.RestTemplate
build()
Build a newRestTemplate
instance and configure it using this builder.<T extends org.springframework.web.client.RestTemplate>
Tbuild(Class<T> restTemplateClass)
Build a newRestTemplate
instance of the specified type and configure it using this builder.<T extends org.springframework.web.client.RestTemplate>
Tconfigure(T restTemplate)
Configure the providedRestTemplate
instance using this builder.RestTemplateBuilder
customizers(Collection<? extends RestTemplateCustomizer> restTemplateCustomizers)
Set theRestTemplateCustomizers
that should be applied to theRestTemplate
.RestTemplateBuilder
customizers(RestTemplateCustomizer... restTemplateCustomizers)
Set theRestTemplateCustomizers
that should be applied to theRestTemplate
.RestTemplateBuilder
defaultMessageConverters()
Set theHttpMessageConverters
that should be used with theRestTemplate
to the default set.RestTemplateBuilder
detectRequestFactory(boolean detectRequestFactory)
Set if theClientHttpRequestFactory
should be detected based on the classpath.RestTemplateBuilder
errorHandler(org.springframework.web.client.ResponseErrorHandler errorHandler)
Set theResponseErrorHandler
that should be used with theRestTemplate
.RestTemplateBuilder
interceptors(Collection<org.springframework.http.client.ClientHttpRequestInterceptor> interceptors)
Set theClientHttpRequestInterceptors
that should be used with theRestTemplate
.RestTemplateBuilder
interceptors(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors)
Set theClientHttpRequestInterceptors
that should be used with theRestTemplate
.RestTemplateBuilder
messageConverters(Collection<? extends org.springframework.http.converter.HttpMessageConverter<?>> messageConverters)
Set theHttpMessageConverters
that should be used with theRestTemplate
.RestTemplateBuilder
messageConverters(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters)
Set theHttpMessageConverters
that should be used with theRestTemplate
.RestTemplateBuilder
requestFactory(Class<? extends org.springframework.http.client.ClientHttpRequestFactory> requestFactory)
Set theClientHttpRequestFactory
class that should be used with theRestTemplate
.RestTemplateBuilder
requestFactory(Supplier<org.springframework.http.client.ClientHttpRequestFactory> requestFactorySupplier)
Set theSupplier
ofClientHttpRequestFactory
that should be called each time webuild()
a newRestTemplate
instance.RestTemplateBuilder
rootUri(String rootUri)
Set a root URL that should be applied to each request that starts with'/'
.RestTemplateBuilder
setConnectTimeout(int connectTimeout)
Deprecated.since 2.1.0 in favor ofsetConnectTimeout(Duration)
RestTemplateBuilder
setConnectTimeout(Duration connectTimeout)
Sets the connection timeout on the underlyingClientHttpRequestFactory
.RestTemplateBuilder
setReadTimeout(int readTimeout)
Deprecated.since 2.1.0 in favor ofsetReadTimeout(Duration)
RestTemplateBuilder
setReadTimeout(Duration readTimeout)
Sets the read timeout on the underlyingClientHttpRequestFactory
.RestTemplateBuilder
uriTemplateHandler(org.springframework.web.util.UriTemplateHandler uriTemplateHandler)
Set theUriTemplateHandler
that should be used with theRestTemplate
.
Constructor Detail
RestTemplateBuilder
public RestTemplateBuilder(RestTemplateCustomizer... customizers)
Create a newRestTemplateBuilder
instance.- Parameters:
customizers
- anyRestTemplateCustomizers
that should be applied when theRestTemplate
is built
Method Detail
detectRequestFactory
public RestTemplateBuilder detectRequestFactory(boolean 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
public RestTemplateBuilder rootUri(String rootUri)
Set a root URL that should be applied to each request that starts with'/'
. SeeRootUriTemplateHandler
for details.- 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:
additionalMessageConverters(HttpMessageConverter...)
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(HttpMessageConverter...)
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:
messageConverters(HttpMessageConverter...)
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:
messageConverters(HttpMessageConverter...)
defaultMessageConverters
public RestTemplateBuilder 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:
messageConverters(HttpMessageConverter...)
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:
additionalInterceptors(ClientHttpRequestInterceptor...)
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(ClientHttpRequestInterceptor...)
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:
interceptors(ClientHttpRequestInterceptor...)
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:
interceptors(ClientHttpRequestInterceptor...)
requestFactory
public RestTemplateBuilder requestFactory(Class<? extends org.springframework.http.client.ClientHttpRequestFactory> requestFactory)
Set theClientHttpRequestFactory
class that should be used with theRestTemplate
.- Parameters:
requestFactory
- the request factory to use- Returns:
- a new builder instance
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
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
basicAuthorization
@Deprecated public RestTemplateBuilder basicAuthorization(String username, String password)
Deprecated.since 2.1.0 in favor ofbasicAuthentication(String username, String password)
Add HTTP basic authentication to requests. SeeBasicAuthenticationInterceptor
for details.- Parameters:
username
- the user namepassword
- the password- Returns:
- a new builder instance
basicAuthentication
public RestTemplateBuilder basicAuthentication(String username, String password)
Add HTTP basic authentication to requests. SeeBasicAuthenticationInterceptor
for details.- Parameters:
username
- the user namepassword
- the password- Returns:
- a new builder instance
- Since:
- 2.1.0
customizers
public RestTemplateBuilder customizers(RestTemplateCustomizer... restTemplateCustomizers)
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:
restTemplateCustomizers
- the customizers to set- Returns:
- a new builder instance
- See Also:
additionalCustomizers(RestTemplateCustomizer...)
customizers
public RestTemplateBuilder customizers(Collection<? extends RestTemplateCustomizer> restTemplateCustomizers)
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:
restTemplateCustomizers
- the customizers to set- Returns:
- a new builder instance
- See Also:
additionalCustomizers(RestTemplateCustomizer...)
additionalCustomizers
public RestTemplateBuilder additionalCustomizers(RestTemplateCustomizer... restTemplateCustomizers)
AddRestTemplateCustomizers
that should be applied to theRestTemplate
. Customizers are applied in the order that they were added after builder configuration has been applied.- Parameters:
restTemplateCustomizers
- the customizers to add- Returns:
- a new builder instance
- See Also:
customizers(RestTemplateCustomizer...)
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:
customizers(RestTemplateCustomizer...)
setConnectTimeout
public RestTemplateBuilder setConnectTimeout(Duration connectTimeout)
Sets the connection timeout on the underlyingClientHttpRequestFactory
.- Parameters:
connectTimeout
- the connection timeout- Returns:
- a new builder instance.
- Since:
- 2.1.0
setConnectTimeout
@Deprecated public RestTemplateBuilder setConnectTimeout(int connectTimeout)
Deprecated.since 2.1.0 in favor ofsetConnectTimeout(Duration)
Sets the connection timeout in milliseconds on the underlyingClientHttpRequestFactory
.- Parameters:
connectTimeout
- the connection timeout in milliseconds- Returns:
- a new builder instance.
setReadTimeout
public RestTemplateBuilder setReadTimeout(Duration readTimeout)
Sets the read timeout on the underlyingClientHttpRequestFactory
.- Parameters:
readTimeout
- the read timeout- Returns:
- a new builder instance.
- Since:
- 2.1.0
setReadTimeout
@Deprecated public RestTemplateBuilder setReadTimeout(int readTimeout)
Deprecated.since 2.1.0 in favor ofsetReadTimeout(Duration)
Sets the read timeout in milliseconds on the underlyingClientHttpRequestFactory
.- Parameters:
readTimeout
- the read timeout in milliseconds- Returns:
- a new builder instance.
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(Class)
,configure(RestTemplate)
build
public <T extends org.springframework.web.client.RestTemplate> T build(Class<T> restTemplateClass)
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:
build()
,configure(RestTemplate)
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:
build()
,build(Class)