类 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.
- 从以下版本开始:
- 1.4.0
- 作者:
- Stephane Nicoll, Phillip Webb, Andy Wilkinson, Brian Clozel, Dmytro Nosan, Kevin Strijbos, Ilya Lukyanovich, Scott Frederick, Yanming Zhou
-
构造器概要
构造器构造器说明RestTemplateBuilder(RestTemplateCustomizer... customizers) Create a newRestTemplateBuilderinstance. -
方法概要
修饰符和类型方法说明additionalCustomizers(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.connectTimeout(Duration connectTimeout) Sets the connection timeout on the underlyingClientHttpRequestFactory.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 outgoingClientHttpRequest.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.readTimeout(Duration readTimeout) Sets the read timeout on the underlyingClientHttpRequestFactory.Sets the redirect strategy on the underlyingClientHttpRequestFactory.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> requestFactoryType) Set theClientHttpRequestFactoryclass that should be used with theRestTemplate.requestFactory(Function<ClientHttpRequestFactorySettings, ClientHttpRequestFactory> requestFactoryFunction) 已过时, 待删除: 此 API 元素将从以后的版本中删除。requestFactory(Supplier<ClientHttpRequestFactory> requestFactorySupplier) Set theSupplierofClientHttpRequestFactorythat should be called each time webuild()a newRestTemplateinstance.Return aClientHttpRequestFactoryBuilderinstance using the settings of this builder.requestFactoryBuilder(ClientHttpRequestFactoryBuilder<?> requestFactoryBuilder) Set theClientHttpRequestFactoryBuilderthat should be used each time webuild()a newRestTemplateinstance.requestFactorySettings(UnaryOperator<ClientHttpRequestFactorySettings> requestFactorySettingsCustomizer) Update theClientHttpRequestFactorySettingsusing 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) 已过时, 待删除: 此 API 元素将从以后的版本中删除。since 3.4.0 for removal in 4.0.0 in favor ofconnectTimeout(Duration)setReadTimeout(Duration readTimeout) 已过时, 待删除: 此 API 元素将从以后的版本中删除。since 3.4.0 for removal in 4.0.0 in favor ofreadTimeout(Duration)setSslBundle(SslBundle sslBundle) 已过时, 待删除: 此 API 元素将从以后的版本中删除。since 3.4.0 for removal in 4.0.0 in favor ofsslBundle(SslBundle)Sets the SSL bundle on the underlyingClientHttpRequestFactory.uriTemplateHandler(UriTemplateHandler uriTemplateHandler) Set theUriTemplateHandlerthat should be used with theRestTemplate.
-
构造器详细资料
-
RestTemplateBuilder
Create a newRestTemplateBuilderinstance.- 参数:
customizers- anyRestTemplateCustomizersthat should be applied when theRestTemplateis built
-
-
方法详细资料
-
detectRequestFactory
Set if theClientHttpRequestFactoryshould be detected based on the classpath. Default iftrue.- 参数:
detectRequestFactory- if theClientHttpRequestFactoryshould be detected- 返回:
- 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 whenStringvariants of theRestTemplatemethods are used for specifying the request URL.- 参数:
rootUri- the root URI ornull- 返回:
- 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.- 参数:
messageConverters- the converters to set- 返回:
- a new builder instance
- 另请参阅:
-
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.- 参数:
messageConverters- the converters to set- 返回:
- a new builder instance
- 另请参阅:
-
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.- 参数:
messageConverters- the converters to add- 返回:
- a new builder instance
- 另请参阅:
-
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.- 参数:
messageConverters- the converters to add- 返回:
- a new builder instance
- 另请参阅:
-
defaultMessageConverters
Set theHttpMessageConvertersthat should be used with theRestTemplateto the default set. Calling this method will replace any previously defined converters.- 返回:
- a new builder instance
- 另请参阅:
-
interceptors
Set theClientHttpRequestInterceptorsthat 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
- 另请参阅:
-
interceptors
Set theClientHttpRequestInterceptorsthat 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
Add additionalClientHttpRequestInterceptorsthat should be used with theRestTemplate.- 参数:
interceptors- the interceptors to add- 返回:
- a new builder instance
- 从以下版本开始:
- 1.4.1
- 另请参阅:
-
additionalInterceptors
public RestTemplateBuilder additionalInterceptors(Collection<? extends ClientHttpRequestInterceptor> interceptors) Add additionalClientHttpRequestInterceptorsthat should be used with theRestTemplate.- 参数:
interceptors- the interceptors to add- 返回:
- a new builder instance
- 从以下版本开始:
- 1.4.1
- 另请参阅:
-
requestFactory
public RestTemplateBuilder requestFactory(Class<? extends ClientHttpRequestFactory> requestFactoryType) Set theClientHttpRequestFactoryclass that should be used with theRestTemplate.- 参数:
requestFactoryType- the request factory type to use- 返回:
- a new builder instance
- 另请参阅:
-
requestFactory
public RestTemplateBuilder requestFactory(Supplier<ClientHttpRequestFactory> requestFactorySupplier) Set theSupplierofClientHttpRequestFactorythat should be called each time webuild()a newRestTemplateinstance.- 参数:
requestFactorySupplier- the supplier for the request factory- 返回:
- a new builder instance
- 从以下版本开始:
- 2.0.0
- 另请参阅:
-
requestFactory
@Deprecated(since="3.4.0", forRemoval=true) public RestTemplateBuilder requestFactory(Function<ClientHttpRequestFactorySettings, ClientHttpRequestFactory> requestFactoryFunction) 已过时, 待删除: 此 API 元素将从以后的版本中删除。since 3.4.0 for removal in 4.0.0 in favor ofrequestFactoryBuilder(ClientHttpRequestFactoryBuilder)Set the request factory function that should be called to provide aClientHttpRequestFactoryeach time webuild()a newRestTemplateinstance.- 参数:
requestFactoryFunction- the settings to request factory function- 返回:
- a new builder instance
- 从以下版本开始:
- 3.0.0
- 另请参阅:
-
requestFactoryBuilder
public RestTemplateBuilder requestFactoryBuilder(ClientHttpRequestFactoryBuilder<?> requestFactoryBuilder) Set theClientHttpRequestFactoryBuilderthat should be used each time webuild()a newRestTemplateinstance.- 参数:
requestFactoryBuilder- theClientHttpRequestFactoryBuilderto use- 返回:
- a new builder instance
- 从以下版本开始:
- 3.4.0
- 另请参阅:
-
uriTemplateHandler
Set theUriTemplateHandlerthat should be used with theRestTemplate.- 参数:
uriTemplateHandler- the URI template handler to use- 返回:
- a new builder instance
-
errorHandler
Set theResponseErrorHandlerthat should be used with theRestTemplate.- 参数:
errorHandler- the error handler to use- 返回:
- 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.- 参数:
username- the user namepassword- the password- 返回:
- a new builder instance
- 从以下版本开始:
- 2.1.0
- 另请参阅:
-
basicAuthentication
Add HTTP Basic Authentication to requests with the given username/password pair, unless a custom Authorization header has been set before.- 参数:
username- the user namepassword- the passwordcharset- the charset to use- 返回:
- a new builder instance
- 从以下版本开始:
- 2.2.0
-
defaultHeader
Add a default header that will be set if not already present on the outgoingClientHttpRequest.- 参数:
name- the name of the headervalues- the header values- 返回:
- a new builder instance
- 从以下版本开始:
- 2.2.0
-
requestFactorySettings
public RestTemplateBuilder requestFactorySettings(ClientHttpRequestFactorySettings requestFactorySettings) Sets theClientHttpRequestFactorySettings. This will replace any previously setconnectTimeout,readTimeoutandsslBundlevalues.- 参数:
requestFactorySettings- the request factory settings- 返回:
- a new builder instance
- 从以下版本开始:
- 3.4.0
-
requestFactorySettings
public RestTemplateBuilder requestFactorySettings(UnaryOperator<ClientHttpRequestFactorySettings> requestFactorySettingsCustomizer) Update theClientHttpRequestFactorySettingsusing the given customizer.- 参数:
requestFactorySettingsCustomizer- aUnaryOperatorto update request factory settings- 返回:
- a new builder instance
- 从以下版本开始:
- 3.4.1
-
setConnectTimeout
@Deprecated(since="3.4.0", forRemoval=true) public RestTemplateBuilder setConnectTimeout(Duration connectTimeout) 已过时, 待删除: 此 API 元素将从以后的版本中删除。since 3.4.0 for removal in 4.0.0 in favor ofconnectTimeout(Duration)Sets the connection timeout on the underlyingClientHttpRequestFactory.- 参数:
connectTimeout- the connection timeout- 返回:
- a new builder instance.
- 从以下版本开始:
- 2.1.0
-
connectTimeout
Sets the connection timeout on the underlyingClientHttpRequestFactory.- 参数:
connectTimeout- the connection timeout- 返回:
- a new builder instance.
- 从以下版本开始:
- 3.4.0
-
setReadTimeout
@Deprecated(since="3.4.0", forRemoval=true) public RestTemplateBuilder setReadTimeout(Duration readTimeout) 已过时, 待删除: 此 API 元素将从以后的版本中删除。since 3.4.0 for removal in 4.0.0 in favor ofreadTimeout(Duration)Sets the read timeout on the underlyingClientHttpRequestFactory.- 参数:
readTimeout- the read timeout- 返回:
- a new builder instance.
- 从以下版本开始:
- 2.1.0
-
readTimeout
Sets the read timeout on the underlyingClientHttpRequestFactory.- 参数:
readTimeout- the read timeout- 返回:
- a new builder instance.
- 从以下版本开始:
- 3.4.0
-
redirects
Sets the redirect strategy on the underlyingClientHttpRequestFactory.- 参数:
redirects- the redirect strategy- 返回:
- a new builder instance.
- 从以下版本开始:
- 3.4.1
-
setSslBundle
@Deprecated(since="3.4.0", forRemoval=true) public RestTemplateBuilder setSslBundle(SslBundle sslBundle) 已过时, 待删除: 此 API 元素将从以后的版本中删除。since 3.4.0 for removal in 4.0.0 in favor ofsslBundle(SslBundle)Sets the SSL bundle on the underlyingClientHttpRequestFactory.- 参数:
sslBundle- the SSL bundle- 返回:
- a new builder instance
- 从以下版本开始:
- 3.1.0
-
sslBundle
Sets the SSL bundle on the underlyingClientHttpRequestFactory.- 参数:
sslBundle- the SSL bundle- 返回:
- a new builder instance
- 从以下版本开始:
- 3.4.0
-
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.- 参数:
customizers- the customizers to set- 返回:
- a new builder instance
- 另请参阅:
-
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.- 参数:
customizers- the customizers to set- 返回:
- a new builder instance
- 另请参阅:
-
additionalCustomizers
AddRestTemplateCustomizersthat 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
- 另请参阅:
-
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.- 参数:
customizers- the customizers to add- 返回:
- a new builder instance
- 另请参阅:
-
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.- 参数:
requestCustomizers- the request customizers to set- 返回:
- a new builder instance
- 从以下版本开始:
- 2.2.0
- 另请参阅:
-
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.- 参数:
requestCustomizers- the request customizers to set- 返回:
- a new builder instance
- 从以下版本开始:
- 2.2.0
- 另请参阅:
-
additionalRequestCustomizers
public RestTemplateBuilder additionalRequestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers) Add theRestTemplateRequestCustomizersthat should be applied to theClientHttpRequest. Customizers are applied in the order that they were added.- 参数:
requestCustomizers- the request customizers to add- 返回:
- a new builder instance
- 从以下版本开始:
- 2.2.0
- 另请参阅:
-
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.- 参数:
requestCustomizers- the request customizers to add- 返回:
- a new builder instance
- 从以下版本开始:
- 2.2.0
- 另请参阅:
-
build
Build a newRestTemplateinstance and configure it using this builder.- 返回:
- a configured
RestTemplateinstance. - 另请参阅:
-
build
Build a newRestTemplateinstance of the specified type and configure it using this builder.- 类型参数:
T- the type of rest template- 参数:
restTemplateClass- the template type to create- 返回:
- a configured
RestTemplateinstance. - 另请参阅:
-
configure
Configure the providedRestTemplateinstance using this builder.- 类型参数:
T- the type of rest template- 参数:
restTemplate- theRestTemplateto configure- 返回:
- the rest template instance
- 另请参阅:
-
buildRequestFactory
Build a newClientHttpRequestFactoryinstance using the settings of this builder.- 返回:
- a
ClientHttpRequestFactoryornull - 从以下版本开始:
- 2.2.0
-
requestFactoryBuilder
Return aClientHttpRequestFactoryBuilderinstance using the settings of this builder.- 返回:
- a
ClientHttpRequestFactoryBuilderornull - 从以下版本开始:
- 3.5.0
-
requestFactoryBuilder(ClientHttpRequestFactoryBuilder)