类 RestTemplateBuilder
java.lang.Object
cn.taketoday.web.client.config.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 application this builder is available as a bean and can be
injected whenever a RestTemplate is needed.
- 从以下版本开始:
- 4.0
- 作者:
- Stephane Nicoll, Phillip Webb, Andy Wilkinson, Brian Clozel, Dmytro Nosan, Kevin Strijbos, Ilya Lukyanovich, Harry Yang
-
字段概要
字段修饰符和类型字段说明private final BasicAuthenticationprivate final Set<RestTemplateCustomizer>private final booleanprivate final ResponseErrorHandlerprivate final Set<ClientHttpRequestInterceptor>private final Set<HttpMessageConverter<?>>private final Set<RestTemplateRequestCustomizer<?>>private final Function<ClientHttpRequestFactorySettings,ClientHttpRequestFactory> private final ClientHttpRequestFactorySettingsprivate final Stringprivate final UriTemplateHandler -
构造器概要
构造器限定符构造器说明privateRestTemplateBuilder(ClientHttpRequestFactorySettings requestFactorySettings, boolean detectRequestFactory, String rootUri, Set<HttpMessageConverter<?>> messageConverters, Set<ClientHttpRequestInterceptor> interceptors, Function<ClientHttpRequestFactorySettings, ClientHttpRequestFactory> requestFactorySupplier, UriTemplateHandler uriTemplateHandler, ResponseErrorHandler errorHandler, BasicAuthentication basicAuthentication, Map<String, List<String>> defaultHeaders, Set<RestTemplateCustomizer> customizers, Set<RestTemplateRequestCustomizer<?>> requestCustomizers) RestTemplateBuilder(RestTemplateCustomizer... customizers) Create a newRestTemplateBuilderinstance. -
方法概要
修饰符和类型方法说明private voidaddClientHttpRequestInitializer(RestTemplate restTemplate) additionalCustomizers(RestTemplateCustomizer... customizers) AddRestTemplateCustomizersthat should be applied to theRestTemplate.additionalCustomizers(Collection<? extends RestTemplateCustomizer> customizers) AddRestTemplateCustomizersthat should be applied to theRestTemplate.additionalInterceptors(ClientHttpRequestInterceptor... interceptors) Add additionalClientHttpRequestInterceptorsthat should be used with theRestTemplate.additionalInterceptors(Collection<? extends ClientHttpRequestInterceptor> interceptors) Add additionalClientHttpRequestInterceptorsthat should be used with theRestTemplate.additionalMessageConverters(HttpMessageConverter<?>... messageConverters) Add additionalHttpMessageConvertersthat should be used with theRestTemplate.additionalMessageConverters(Collection<? extends HttpMessageConverter<?>> messageConverters) Add additionalHttpMessageConvertersthat should be used with theRestTemplate.additionalRequestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers) Add theRestTemplateRequestCustomizersthat should be applied to theClientHttpRequest.additionalRequestCustomizers(Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers) Add theRestTemplateRequestCustomizersthat should be applied to theClientHttpRequest.private static <T> Set<T>append(Collection<? extends T> collection, Collection<? extends T> additions) 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.private static <T> List<T>copiedListOf(T[] items) private <T> Set<T>copiedSetOf(Collection<? extends T> collection) private <T> Set<T>copiedSetOf(T... items) customizers(RestTemplateCustomizer... customizers) Set theRestTemplateCustomizersthat should be applied to theRestTemplate.customizers(Collection<? extends 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 outgoingHttpClientRequest.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(ClientHttpRequestInterceptor... interceptors) Set theClientHttpRequestInterceptorsthat should be used with theRestTemplate.interceptors(Collection<ClientHttpRequestInterceptor> interceptors) Set theClientHttpRequestInterceptorsthat should be used with theRestTemplate.messageConverters(HttpMessageConverter<?>... messageConverters) Set theHttpMessageConvertersthat should be used with theRestTemplate.messageConverters(Collection<? extends HttpMessageConverter<?>> messageConverters) Set theHttpMessageConvertersthat should be used with theRestTemplate.requestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers) Set theRestTemplateRequestCustomizersthat should be applied to theClientHttpRequest.requestCustomizers(Collection<? extends 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) Set the request factory function that should be called to provide aClientHttpRequestFactoryeach time webuild()a newRestTemplateinstance.requestFactory(Supplier<ClientHttpRequestFactory> requestFactory) Set theSupplierofClientHttpRequestFactorythat should be called each time webuild()a newRestTemplateinstance.Set a root URL that should be applied to each request that starts with'/'.setConnectTimeout(Duration connectTimeout) Sets the connection timeout on the underlyingClientHttpRequestFactory.setReadTimeout(Duration readTimeout) Sets the read timeout on the underlyingClientHttpRequestFactory.setSslBundle(cn.taketoday.core.ssl.SslBundle sslBundle) Sets the SSL bundle on the underlyingClientHttpRequestFactory.uriTemplateHandler(UriTemplateHandler uriTemplateHandler) Set theUriTemplateHandlerthat should be used with theRestTemplate.
-
字段详细资料
-
requestFactorySettings
-
detectRequestFactory
private final boolean detectRequestFactory -
rootUri
-
messageConverters
-
interceptors
-
requestFactory
@Nullable private final Function<ClientHttpRequestFactorySettings,ClientHttpRequestFactory> requestFactory -
uriTemplateHandler
-
errorHandler
-
basicAuthentication
-
defaultHeaders
-
customizers
-
requestCustomizers
-
-
构造器详细资料
-
RestTemplateBuilder
Create a newRestTemplateBuilderinstance.- 参数:
customizers- anyRestTemplateCustomizersthat should be applied when theRestTemplateis built
-
RestTemplateBuilder
private RestTemplateBuilder(ClientHttpRequestFactorySettings requestFactorySettings, boolean detectRequestFactory, @Nullable String rootUri, @Nullable Set<HttpMessageConverter<?>> messageConverters, Set<ClientHttpRequestInterceptor> interceptors, @Nullable Function<ClientHttpRequestFactorySettings, ClientHttpRequestFactory> requestFactorySupplier, @Nullable UriTemplateHandler uriTemplateHandler, @Nullable ResponseErrorHandler errorHandler, @Nullable BasicAuthentication basicAuthentication, Map<String, List<String>> defaultHeaders, Set<RestTemplateCustomizer> customizers, Set<RestTemplateRequestCustomizer<?>> requestCustomizers)
-
-
方法详细资料
-
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'/'. Since this works by adding aUriTemplateHandlerto theRestTemplate, the root URL will only apply whenStringvariants of theRestTemplatemethods are used for specifying the request URL. SeeRootUriTemplateHandlerfor details.- 参数:
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
- 另请参阅:
-
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
- 另请参阅:
-
additionalInterceptors
Add additionalClientHttpRequestInterceptorsthat should be used with theRestTemplate.- 参数:
interceptors- the interceptors to add- 返回:
- a new builder instance
- 另请参阅:
-
additionalInterceptors
public RestTemplateBuilder additionalInterceptors(Collection<? extends ClientHttpRequestInterceptor> interceptors) Add additionalClientHttpRequestInterceptorsthat should be used with theRestTemplate.- 参数:
interceptors- the interceptors to add- 返回:
- a new builder instance
- 另请参阅:
-
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
Set theSupplierofClientHttpRequestFactorythat should be called each time webuild()a newRestTemplateinstance.- 参数:
requestFactory- the supplier for the request factory- 返回:
- a new builder instance
-
requestFactory
public RestTemplateBuilder requestFactory(Function<ClientHttpRequestFactorySettings, ClientHttpRequestFactory> requestFactoryFunction) 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
- 另请参阅:
-
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
- 另请参阅:
-
basicAuthentication
public RestTemplateBuilder basicAuthentication(String username, String password, @Nullable Charset charset) 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
-
defaultHeader
Add a default header that will be set if not already present on the outgoingHttpClientRequest.- 参数:
name- the name of the headervalues- the header values- 返回:
- a new builder instance
-
setConnectTimeout
Sets the connection timeout on the underlyingClientHttpRequestFactory.- 参数:
connectTimeout- the connection timeout- 返回:
- a new builder instance.
-
setReadTimeout
Sets the read timeout on the underlyingClientHttpRequestFactory.- 参数:
readTimeout- the read timeout- 返回:
- a new builder instance.
-
setSslBundle
Sets the SSL bundle on the underlyingClientHttpRequestFactory.- 参数:
sslBundle- the SSL bundle- 返回:
- 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
- 另请参阅:
-
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
- 另请参阅:
-
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
- 另请参阅:
-
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
- 另请参阅:
-
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
- 另请参阅:
-
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
-
addClientHttpRequestInitializer
-
copiedSetOf
-
copiedSetOf
-
copiedListOf
-
append
private static <T> Set<T> append(@Nullable Collection<? extends T> collection, @Nullable Collection<? extends T> additions) -
append
-