类 RestTemplateBuilder


  • public class RestTemplateBuilder
    extends Object
    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
    • 方法详细资料

      • detectRequestFactory

        public RestTemplateBuilder detectRequestFactory​(boolean detectRequestFactory)
        Set if the ClientHttpRequestFactory should be detected based on the classpath. Default if true.
        参数:
        detectRequestFactory - if the ClientHttpRequestFactory should be detected
        返回:
        a new builder instance
      • messageConverters

        public RestTemplateBuilder messageConverters​(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters)
        Set the 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 set
        返回:
        a new builder instance
        另请参阅:
        additionalMessageConverters(HttpMessageConverter...)
      • messageConverters

        public RestTemplateBuilder messageConverters​(Collection<? extends org.springframework.http.converter.HttpMessageConverter<?>> messageConverters)
        Set the 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 set
        返回:
        a new builder instance
        另请参阅:
        additionalMessageConverters(HttpMessageConverter...)
      • additionalMessageConverters

        public RestTemplateBuilder additionalMessageConverters​(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters)
        Add additional HttpMessageConverters that should be used with the RestTemplate. Any converters configured on the builder will replace RestTemplate's default converters.
        参数:
        messageConverters - the converters to add
        返回:
        a new builder instance
        另请参阅:
        messageConverters(HttpMessageConverter...)
      • interceptors

        public RestTemplateBuilder interceptors​(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors)
        Set the ClientHttpRequestInterceptors that should be used with the RestTemplate. Setting this value will replace any previously defined interceptors.
        参数:
        interceptors - the interceptors to set
        返回:
        a new builder instance
        从以下版本开始:
        1.4.1
        另请参阅:
        additionalInterceptors(ClientHttpRequestInterceptor...)
      • requestFactory

        public RestTemplateBuilder requestFactory​(Class<? extends org.springframework.http.client.ClientHttpRequestFactory> requestFactory)
        Set the ClientHttpRequestFactory class that should be used with the RestTemplate.
        参数:
        requestFactory - the request factory to use
        返回:
        a new builder instance
      • requestFactory

        public RestTemplateBuilder requestFactory​(Supplier<org.springframework.http.client.ClientHttpRequestFactory> requestFactorySupplier)
        Set the Supplier of ClientHttpRequestFactory that should be called each time we build() a new RestTemplate 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 the UriTemplateHandler that should be used with the RestTemplate.
        参数:
        uriTemplateHandler - the URI template handler to use
        返回:
        a new builder instance
      • errorHandler

        public RestTemplateBuilder errorHandler​(org.springframework.web.client.ResponseErrorHandler errorHandler)
        Set the ResponseErrorHandler that should be used with the RestTemplate.
        参数:
        errorHandler - the error handler to use
        返回:
        a new builder instance
      • basicAuthentication

        public RestTemplateBuilder basicAuthentication​(String username,
                                                       String password)
        Add HTTP basic authentication to requests. See BasicAuthenticationInterceptor for details.
        参数:
        username - the user name
        password - the password
        返回:
        a new builder instance
        从以下版本开始:
        2.1.0
      • setConnectTimeout

        public RestTemplateBuilder setConnectTimeout​(Duration connectTimeout)
        Sets the connection timeout on the underlying ClientHttpRequestFactory.
        参数:
        connectTimeout - the connection timeout
        返回:
        a new builder instance.
        从以下版本开始:
        2.1.0
      • setReadTimeout

        public RestTemplateBuilder setReadTimeout​(Duration readTimeout)
        Sets the read timeout on the underlying ClientHttpRequestFactory.
        参数:
        readTimeout - the read timeout
        返回:
        a new builder instance.
        从以下版本开始:
        2.1.0
      • build

        public org.springframework.web.client.RestTemplate build()
        Build a new RestTemplate 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 new RestTemplate 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 provided RestTemplate instance using this builder.
        类型参数:
        T - the type of rest template
        参数:
        restTemplate - the RestTemplate to configure
        返回:
        the rest template instance
        另请参阅:
        build(), build(Class)