public class RestTemplateBuilder
extends java.lang.Object
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.
Constructor and Description |
---|
RestTemplateBuilder(RestTemplateCustomizer... customizers)
Create a new
RestTemplateBuilder instance. |
Modifier and Type | Method and Description |
---|---|
RestTemplateBuilder |
additionalCustomizers(java.util.Collection<? extends RestTemplateCustomizer> customizers)
Add
RestTemplateCustomizers that should be applied
to the RestTemplate . |
RestTemplateBuilder |
additionalCustomizers(RestTemplateCustomizer... customizers)
Add
RestTemplateCustomizers that should be applied
to the RestTemplate . |
RestTemplateBuilder |
additionalInterceptors(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors)
Add additional
ClientHttpRequestInterceptors
that should be used with the RestTemplate . |
RestTemplateBuilder |
additionalInterceptors(java.util.Collection<? extends org.springframework.http.client.ClientHttpRequestInterceptor> interceptors)
Add additional
ClientHttpRequestInterceptors
that should be used with the RestTemplate . |
RestTemplateBuilder |
additionalMessageConverters(java.util.Collection<? extends org.springframework.http.converter.HttpMessageConverter<?>> messageConverters)
Add additional
HttpMessageConverters that should be
used with the RestTemplate . |
RestTemplateBuilder |
additionalMessageConverters(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters)
Add additional
HttpMessageConverters that should be
used with the RestTemplate . |
RestTemplateBuilder |
additionalRequestCustomizers(java.util.Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers)
Add the
RestTemplateRequestCustomizers that
should be applied to the ClientHttpRequest . |
RestTemplateBuilder |
additionalRequestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers)
Add the
RestTemplateRequestCustomizers that
should be applied to the ClientHttpRequest . |
RestTemplateBuilder |
basicAuthentication(java.lang.String username,
java.lang.String password)
Add HTTP Basic Authentication to requests with the given username/password pair,
unless a custom Authorization header has been set before.
|
RestTemplateBuilder |
basicAuthentication(java.lang.String username,
java.lang.String password,
java.nio.charset.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 new
RestTemplate instance and configure it using this builder. |
<T extends org.springframework.web.client.RestTemplate> |
build(java.lang.Class<T> restTemplateClass)
Build a new
RestTemplate instance of the specified type and configure it
using this builder. |
org.springframework.http.client.ClientHttpRequestFactory |
buildRequestFactory()
Build a new
ClientHttpRequestFactory instance using the settings of this
builder. |
<T extends org.springframework.web.client.RestTemplate> |
configure(T restTemplate)
Configure the provided
RestTemplate instance using this builder. |
RestTemplateBuilder |
customizers(java.util.Collection<? extends RestTemplateCustomizer> customizers)
Set the
RestTemplateCustomizers that should be
applied to the RestTemplate . |
RestTemplateBuilder |
customizers(RestTemplateCustomizer... customizers)
Set the
RestTemplateCustomizers that should be
applied to the RestTemplate . |
RestTemplateBuilder |
defaultHeader(java.lang.String name,
java.lang.String... values)
Add a default header that will be set if not already present on the outgoing
ClientHttpRequest . |
RestTemplateBuilder |
defaultMessageConverters()
Set the
HttpMessageConverters that should be used with
the RestTemplate to the default set. |
RestTemplateBuilder |
detectRequestFactory(boolean detectRequestFactory)
Set if the
ClientHttpRequestFactory should be detected based on the
classpath. |
RestTemplateBuilder |
errorHandler(org.springframework.web.client.ResponseErrorHandler errorHandler)
Set the
ResponseErrorHandler that should be used with the
RestTemplate . |
RestTemplateBuilder |
interceptors(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors)
Set the
ClientHttpRequestInterceptors that
should be used with the RestTemplate . |
RestTemplateBuilder |
interceptors(java.util.Collection<org.springframework.http.client.ClientHttpRequestInterceptor> interceptors)
Set the
ClientHttpRequestInterceptors that
should be used with the RestTemplate . |
RestTemplateBuilder |
messageConverters(java.util.Collection<? extends org.springframework.http.converter.HttpMessageConverter<?>> messageConverters)
Set the
HttpMessageConverters that should be used with
the RestTemplate . |
RestTemplateBuilder |
messageConverters(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters)
Set the
HttpMessageConverters that should be used with
the RestTemplate . |
RestTemplateBuilder |
requestCustomizers(java.util.Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers)
Set the
RestTemplateRequestCustomizers that
should be applied to the ClientHttpRequest . |
RestTemplateBuilder |
requestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers)
Set the
RestTemplateRequestCustomizers that
should be applied to the ClientHttpRequest . |
RestTemplateBuilder |
requestFactory(java.lang.Class<? extends org.springframework.http.client.ClientHttpRequestFactory> requestFactory)
Set the
ClientHttpRequestFactory class that should be used with the
RestTemplate . |
RestTemplateBuilder |
requestFactory(java.util.function.Supplier<org.springframework.http.client.ClientHttpRequestFactory> requestFactory)
Set the
Supplier of ClientHttpRequestFactory that should be called
each time we build() a new RestTemplate instance. |
RestTemplateBuilder |
rootUri(java.lang.String rootUri)
Set a root URL that should be applied to each request that starts with
'/' . |
RestTemplateBuilder |
setBufferRequestBody(boolean bufferRequestBody)
Sets if the underlying
ClientHttpRequestFactory should buffer the
request body internally. |
RestTemplateBuilder |
setConnectTimeout(java.time.Duration connectTimeout)
Sets the connection timeout on the underlying
ClientHttpRequestFactory . |
RestTemplateBuilder |
setReadTimeout(java.time.Duration readTimeout)
Sets the read timeout on the underlying
ClientHttpRequestFactory . |
RestTemplateBuilder |
uriTemplateHandler(org.springframework.web.util.UriTemplateHandler uriTemplateHandler)
Set the
UriTemplateHandler that should be used with the
RestTemplate . |
public RestTemplateBuilder(RestTemplateCustomizer... customizers)
RestTemplateBuilder
instance.customizers
- any RestTemplateCustomizers
that
should be applied when the RestTemplate
is builtpublic RestTemplateBuilder detectRequestFactory(boolean detectRequestFactory)
ClientHttpRequestFactory
should be detected based on the
classpath. Default if true
.detectRequestFactory
- if the ClientHttpRequestFactory
should be
detectedpublic RestTemplateBuilder rootUri(java.lang.String rootUri)
'/'
.
Since this works by adding a UriTemplateHandler
to the
RestTemplate
, the root URL will only apply when String
variants of
the RestTemplate
methods are used for specifying the request URL. See
RootUriTemplateHandler
for details.rootUri
- the root URI or null
public RestTemplateBuilder messageConverters(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters)
HttpMessageConverters
that should be used with
the RestTemplate
. Setting this value will replace any previously configured
converters and any converters configured on the builder will replace RestTemplate's
default converters.messageConverters
- the converters to setadditionalMessageConverters(HttpMessageConverter...)
public RestTemplateBuilder messageConverters(java.util.Collection<? extends org.springframework.http.converter.HttpMessageConverter<?>> messageConverters)
HttpMessageConverters
that should be used with
the RestTemplate
. Setting this value will replace any previously configured
converters and any converters configured on the builder will replace RestTemplate's
default converters.messageConverters
- the converters to setadditionalMessageConverters(HttpMessageConverter...)
public RestTemplateBuilder additionalMessageConverters(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters)
HttpMessageConverters
that should be
used with the RestTemplate
. Any converters configured on the builder will
replace RestTemplate's default converters.messageConverters
- the converters to addmessageConverters(HttpMessageConverter...)
public RestTemplateBuilder additionalMessageConverters(java.util.Collection<? extends org.springframework.http.converter.HttpMessageConverter<?>> messageConverters)
HttpMessageConverters
that should be
used with the RestTemplate
. Any converters configured on the builder will
replace RestTemplate's default converters.messageConverters
- the converters to addmessageConverters(HttpMessageConverter...)
public RestTemplateBuilder defaultMessageConverters()
HttpMessageConverters
that should be used with
the RestTemplate
to the default set. Calling this method will replace any
previously defined converters.messageConverters(HttpMessageConverter...)
public RestTemplateBuilder interceptors(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors)
ClientHttpRequestInterceptors
that
should be used with the RestTemplate
. Setting this value will replace any
previously defined interceptors.interceptors
- the interceptors to setadditionalInterceptors(ClientHttpRequestInterceptor...)
public RestTemplateBuilder interceptors(java.util.Collection<org.springframework.http.client.ClientHttpRequestInterceptor> interceptors)
ClientHttpRequestInterceptors
that
should be used with the RestTemplate
. Setting this value will replace any
previously defined interceptors.interceptors
- the interceptors to setadditionalInterceptors(ClientHttpRequestInterceptor...)
public RestTemplateBuilder additionalInterceptors(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors)
ClientHttpRequestInterceptors
that should be used with the RestTemplate
.interceptors
- the interceptors to addinterceptors(ClientHttpRequestInterceptor...)
public RestTemplateBuilder additionalInterceptors(java.util.Collection<? extends org.springframework.http.client.ClientHttpRequestInterceptor> interceptors)
ClientHttpRequestInterceptors
that should be used with the RestTemplate
.interceptors
- the interceptors to addinterceptors(ClientHttpRequestInterceptor...)
public RestTemplateBuilder requestFactory(java.lang.Class<? extends org.springframework.http.client.ClientHttpRequestFactory> requestFactory)
ClientHttpRequestFactory
class that should be used with the
RestTemplate
.requestFactory
- the request factory to usepublic RestTemplateBuilder requestFactory(java.util.function.Supplier<org.springframework.http.client.ClientHttpRequestFactory> requestFactory)
Supplier
of ClientHttpRequestFactory
that should be called
each time we build()
a new RestTemplate
instance.requestFactory
- the supplier for the request factorypublic RestTemplateBuilder uriTemplateHandler(org.springframework.web.util.UriTemplateHandler uriTemplateHandler)
UriTemplateHandler
that should be used with the
RestTemplate
.uriTemplateHandler
- the URI template handler to usepublic RestTemplateBuilder errorHandler(org.springframework.web.client.ResponseErrorHandler errorHandler)
ResponseErrorHandler
that should be used with the
RestTemplate
.errorHandler
- the error handler to usepublic RestTemplateBuilder basicAuthentication(java.lang.String username, java.lang.String password)
username
- the user namepassword
- the passwordbasicAuthentication(String, String, Charset)
public RestTemplateBuilder basicAuthentication(java.lang.String username, java.lang.String password, java.nio.charset.Charset charset)
username
- the user namepassword
- the passwordcharset
- the charset to usepublic RestTemplateBuilder defaultHeader(java.lang.String name, java.lang.String... values)
ClientHttpRequest
.name
- the name of the headervalues
- the header valuespublic RestTemplateBuilder setConnectTimeout(java.time.Duration connectTimeout)
ClientHttpRequestFactory
.connectTimeout
- the connection timeoutpublic RestTemplateBuilder setReadTimeout(java.time.Duration readTimeout)
ClientHttpRequestFactory
.readTimeout
- the read timeoutpublic RestTemplateBuilder setBufferRequestBody(boolean bufferRequestBody)
ClientHttpRequestFactory
should buffer the
request body internally.bufferRequestBody
- value of the bufferRequestBody parameterSimpleClientHttpRequestFactory.setBufferRequestBody(boolean)
,
HttpComponentsClientHttpRequestFactory.setBufferRequestBody(boolean)
public RestTemplateBuilder customizers(RestTemplateCustomizer... customizers)
RestTemplateCustomizers
that should be
applied to the RestTemplate
. 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.customizers
- the customizers to setadditionalCustomizers(RestTemplateCustomizer...)
public RestTemplateBuilder customizers(java.util.Collection<? extends RestTemplateCustomizer> customizers)
RestTemplateCustomizers
that should be
applied to the RestTemplate
. 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.customizers
- the customizers to setadditionalCustomizers(RestTemplateCustomizer...)
public RestTemplateBuilder additionalCustomizers(RestTemplateCustomizer... customizers)
RestTemplateCustomizers
that should be applied
to the RestTemplate
. Customizers are applied in the order that they were
added after builder configuration has been applied.customizers
- the customizers to addcustomizers(RestTemplateCustomizer...)
public RestTemplateBuilder additionalCustomizers(java.util.Collection<? extends RestTemplateCustomizer> customizers)
RestTemplateCustomizers
that should be applied
to the RestTemplate
. Customizers are applied in the order that they were
added after builder configuration has been applied.customizers
- the customizers to addcustomizers(RestTemplateCustomizer...)
public RestTemplateBuilder requestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers)
RestTemplateRequestCustomizers
that
should be applied to the ClientHttpRequest
. Customizers are applied in the
order that they were added. Setting this value will replace any previously
configured request customizers.requestCustomizers
- the request customizers to setadditionalRequestCustomizers(RestTemplateRequestCustomizer...)
public RestTemplateBuilder requestCustomizers(java.util.Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers)
RestTemplateRequestCustomizers
that
should be applied to the ClientHttpRequest
. Customizers are applied in the
order that they were added. Setting this value will replace any previously
configured request customizers.requestCustomizers
- the request customizers to setadditionalRequestCustomizers(RestTemplateRequestCustomizer...)
public RestTemplateBuilder additionalRequestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers)
RestTemplateRequestCustomizers
that
should be applied to the ClientHttpRequest
. Customizers are applied in the
order that they were added.requestCustomizers
- the request customizers to addrequestCustomizers(RestTemplateRequestCustomizer...)
public RestTemplateBuilder additionalRequestCustomizers(java.util.Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers)
RestTemplateRequestCustomizers
that
should be applied to the ClientHttpRequest
. Customizers are applied in the
order that they were added.requestCustomizers
- the request customizers to addrequestCustomizers(Collection)
public org.springframework.web.client.RestTemplate build()
RestTemplate
instance and configure it using this builder.RestTemplate
instance.build(Class)
,
configure(RestTemplate)
public <T extends org.springframework.web.client.RestTemplate> T build(java.lang.Class<T> restTemplateClass)
RestTemplate
instance of the specified type and configure it
using this builder.T
- the type of rest templaterestTemplateClass
- the template type to createRestTemplate
instance.build()
,
configure(RestTemplate)
public <T extends org.springframework.web.client.RestTemplate> T configure(T restTemplate)
RestTemplate
instance using this builder.T
- the type of rest templaterestTemplate
- the RestTemplate
to configurebuild()
,
build(Class)
public org.springframework.http.client.ClientHttpRequestFactory buildRequestFactory()
ClientHttpRequestFactory
instance using the settings of this
builder.ClientHttpRequestFactory
or null