类 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.- 从以下版本开始:
- 1.4.0
构造器概要
构造器 构造器 说明 RestTemplateBuilder(RestTemplateCustomizer... customizers)
Create a newRestTemplateBuilder
instance.
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 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)
已过时。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)
已过时。since 2.1.0 in favor ofsetConnectTimeout(Duration)
RestTemplateBuilder
setConnectTimeout(Duration connectTimeout)
Sets the connection timeout on the underlyingClientHttpRequestFactory
.RestTemplateBuilder
setReadTimeout(int readTimeout)
已过时。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
.
构造器详细资料
RestTemplateBuilder
public RestTemplateBuilder(RestTemplateCustomizer... customizers)
Create a newRestTemplateBuilder
instance.- 参数:
customizers
- anyRestTemplateCustomizers
that should be applied when theRestTemplate
is built
方法详细资料
detectRequestFactory
public RestTemplateBuilder detectRequestFactory(boolean detectRequestFactory)
Set if theClientHttpRequestFactory
should be detected based on the classpath. Default iftrue
.- 参数:
detectRequestFactory
- if theClientHttpRequestFactory
should be detected- 返回:
- 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.- 参数:
rootUri
- the root URI ornull
- 返回:
- 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.- 参数:
messageConverters
- the converters to set- 返回:
- a new builder instance
- 另请参阅:
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.- 参数:
messageConverters
- the converters to set- 返回:
- a new builder instance
- 另请参阅:
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.- 参数:
messageConverters
- the converters to add- 返回:
- a new builder instance
- 另请参阅:
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.- 参数:
messageConverters
- the converters to add- 返回:
- a new builder instance
- 另请参阅:
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.- 返回:
- a new builder instance
- 另请参阅:
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.- 参数:
interceptors
- the interceptors to set- 返回:
- a new builder instance
- 从以下版本开始:
- 1.4.1
- 另请参阅:
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.- 参数:
interceptors
- the interceptors to set- 返回:
- a new builder instance
- 从以下版本开始:
- 1.4.1
- 另请参阅:
additionalInterceptors(ClientHttpRequestInterceptor...)
additionalInterceptors
public RestTemplateBuilder additionalInterceptors(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors)
Add additionalClientHttpRequestInterceptors
that should be used with theRestTemplate
.- 参数:
interceptors
- the interceptors to add- 返回:
- a new builder instance
- 从以下版本开始:
- 1.4.1
- 另请参阅:
interceptors(ClientHttpRequestInterceptor...)
additionalInterceptors
public RestTemplateBuilder additionalInterceptors(Collection<? extends org.springframework.http.client.ClientHttpRequestInterceptor> interceptors)
Add additionalClientHttpRequestInterceptors
that should be used with theRestTemplate
.- 参数:
interceptors
- the interceptors to add- 返回:
- a new builder instance
- 从以下版本开始:
- 1.4.1
- 另请参阅:
interceptors(ClientHttpRequestInterceptor...)
requestFactory
public RestTemplateBuilder requestFactory(Class<? extends org.springframework.http.client.ClientHttpRequestFactory> requestFactory)
Set theClientHttpRequestFactory
class that should be used with theRestTemplate
.- 参数:
requestFactory
- the request factory to use- 返回:
- 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.- 参数:
requestFactorySupplier
- the supplier for the request factory- 返回:
- a new builder instance
- 从以下版本开始:
- 2.0.0
uriTemplateHandler
public RestTemplateBuilder uriTemplateHandler(org.springframework.web.util.UriTemplateHandler uriTemplateHandler)
Set theUriTemplateHandler
that should be used with theRestTemplate
.- 参数:
uriTemplateHandler
- the URI template handler to use- 返回:
- a new builder instance
errorHandler
public RestTemplateBuilder errorHandler(org.springframework.web.client.ResponseErrorHandler errorHandler)
Set theResponseErrorHandler
that should be used with theRestTemplate
.- 参数:
errorHandler
- the error handler to use- 返回:
- a new builder instance
basicAuthorization
@Deprecated public RestTemplateBuilder basicAuthorization(String username, String password)
已过时。since 2.1.0 in favor ofbasicAuthentication(String username, String password)
Add HTTP basic authentication to requests. SeeBasicAuthenticationInterceptor
for details.- 参数:
username
- the user namepassword
- the password- 返回:
- a new builder instance
basicAuthentication
public RestTemplateBuilder basicAuthentication(String username, String password)
Add HTTP basic authentication to requests. SeeBasicAuthenticationInterceptor
for details.- 参数:
username
- the user namepassword
- the password- 返回:
- a new builder instance
- 从以下版本开始:
- 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.- 参数:
restTemplateCustomizers
- the customizers to set- 返回:
- a new builder instance
- 另请参阅:
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.- 参数:
restTemplateCustomizers
- the customizers to set- 返回:
- a new builder instance
- 另请参阅:
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.- 参数:
restTemplateCustomizers
- the customizers to add- 返回:
- a new builder instance
- 另请参阅:
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.- 参数:
customizers
- the customizers to add- 返回:
- a new builder instance
- 另请参阅:
customizers(RestTemplateCustomizer...)
setConnectTimeout
public RestTemplateBuilder setConnectTimeout(Duration connectTimeout)
Sets the connection timeout on the underlyingClientHttpRequestFactory
.- 参数:
connectTimeout
- the connection timeout- 返回:
- a new builder instance.
- 从以下版本开始:
- 2.1.0
setConnectTimeout
@Deprecated public RestTemplateBuilder setConnectTimeout(int connectTimeout)
已过时。since 2.1.0 in favor ofsetConnectTimeout(Duration)
Sets the connection timeout in milliseconds on the underlyingClientHttpRequestFactory
.- 参数:
connectTimeout
- the connection timeout in milliseconds- 返回:
- a new builder instance.
setReadTimeout
public RestTemplateBuilder setReadTimeout(Duration readTimeout)
Sets the read timeout on the underlyingClientHttpRequestFactory
.- 参数:
readTimeout
- the read timeout- 返回:
- a new builder instance.
- 从以下版本开始:
- 2.1.0
setReadTimeout
@Deprecated public RestTemplateBuilder setReadTimeout(int readTimeout)
已过时。since 2.1.0 in favor ofsetReadTimeout(Duration)
Sets the read timeout in milliseconds on the underlyingClientHttpRequestFactory
.- 参数:
readTimeout
- the read timeout in milliseconds- 返回:
- a new builder instance.
build
public org.springframework.web.client.RestTemplate build()
Build a newRestTemplate
instance and configure it using this builder.- 返回:
- a configured
RestTemplate
instance. - 另请参阅:
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.- 类型参数:
T
- the type of rest template- 参数:
restTemplateClass
- the template type to create- 返回:
- a configured
RestTemplate
instance. - 另请参阅:
build()
,configure(RestTemplate)
configure
public <T extends org.springframework.web.client.RestTemplate> T configure(T restTemplate)
Configure the providedRestTemplate
instance using this builder.- 类型参数:
T
- the type of rest template- 参数:
restTemplate
- theRestTemplate
to configure- 返回:
- the rest template instance
- 另请参阅:
build()
,build(Class)