Class RestTemplateBuilder

java.lang.Object
org.springframework.boot.web.client.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.

Since:
1.4.0
  • Constructor Details

  • Method Details

    • detectRequestFactory

      public RestTemplateBuilder detectRequestFactory(boolean detectRequestFactory)
      Set if the ClientHttpRequestFactory should be detected based on the classpath. Default if true.
      Parameters:
      detectRequestFactory - if the ClientHttpRequestFactory should be detected
      Returns:
      a new builder instance
    • rootUri

      public RestTemplateBuilder rootUri(String rootUri)
      Set a root URL that should be applied to each request that starts with '/'. 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.
      Parameters:
      rootUri - the root URI or null
      Returns:
      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.
      Parameters:
      messageConverters - the converters to set
      Returns:
      a new builder instance
      See Also:
    • 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.
      Parameters:
      messageConverters - the converters to set
      Returns:
      a new builder instance
      See Also:
    • 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.
      Parameters:
      messageConverters - the converters to add
      Returns:
      a new builder instance
      See Also:
    • additionalMessageConverters

      public RestTemplateBuilder additionalMessageConverters(Collection<? extends 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.
      Parameters:
      messageConverters - the converters to add
      Returns:
      a new builder instance
      See Also:
    • defaultMessageConverters

      public RestTemplateBuilder defaultMessageConverters()
      Set the HttpMessageConverters that should be used with the RestTemplate to the default set. Calling this method will replace any previously defined converters.
      Returns:
      a new builder instance
      See Also:
    • 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.
      Parameters:
      interceptors - the interceptors to set
      Returns:
      a new builder instance
      Since:
      1.4.1
      See Also:
    • interceptors

      public RestTemplateBuilder interceptors(Collection<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.
      Parameters:
      interceptors - the interceptors to set
      Returns:
      a new builder instance
      Since:
      1.4.1
      See Also:
    • additionalInterceptors

      public RestTemplateBuilder additionalInterceptors(org.springframework.http.client.ClientHttpRequestInterceptor... interceptors)
      Add additional ClientHttpRequestInterceptors that should be used with the RestTemplate.
      Parameters:
      interceptors - the interceptors to add
      Returns:
      a new builder instance
      Since:
      1.4.1
      See Also:
    • additionalInterceptors

      public RestTemplateBuilder additionalInterceptors(Collection<? extends org.springframework.http.client.ClientHttpRequestInterceptor> interceptors)
      Add additional ClientHttpRequestInterceptors that should be used with the RestTemplate.
      Parameters:
      interceptors - the interceptors to add
      Returns:
      a new builder instance
      Since:
      1.4.1
      See Also:
    • requestFactory

      public RestTemplateBuilder requestFactory(Class<? extends org.springframework.http.client.ClientHttpRequestFactory> requestFactoryType)
      Set the ClientHttpRequestFactory class that should be used with the RestTemplate.
      Parameters:
      requestFactoryType - the request factory type to use
      Returns:
      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.
      Parameters:
      requestFactorySupplier - the supplier for the request factory
      Returns:
      a new builder instance
      Since:
      2.0.0
    • requestFactory

      public RestTemplateBuilder requestFactory(Function<ClientHttpRequestFactorySettings,org.springframework.http.client.ClientHttpRequestFactory> requestFactoryFunction)
      Set the request factory function that should be called to provide a ClientHttpRequestFactory each time we build() a new RestTemplate instance.
      Parameters:
      requestFactoryFunction - the settings to request factory function
      Returns:
      a new builder instance
      Since:
      3.0.0
      See Also:
    • uriTemplateHandler

      public RestTemplateBuilder uriTemplateHandler(org.springframework.web.util.UriTemplateHandler uriTemplateHandler)
      Set the UriTemplateHandler that should be used with the RestTemplate.
      Parameters:
      uriTemplateHandler - the URI template handler to use
      Returns:
      a new builder instance
    • errorHandler

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

      public RestTemplateBuilder 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.
      Parameters:
      username - the user name
      password - the password
      Returns:
      a new builder instance
      Since:
      2.1.0
      See Also:
    • basicAuthentication

      public RestTemplateBuilder 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.
      Parameters:
      username - the user name
      password - the password
      charset - the charset to use
      Returns:
      a new builder instance
      Since:
      2.2.0
    • defaultHeader

      public RestTemplateBuilder defaultHeader(String name, String... values)
      Add a default header that will be set if not already present on the outgoing ClientHttpRequest.
      Parameters:
      name - the name of the header
      values - the header values
      Returns:
      a new builder instance
      Since:
      2.2.0
    • setConnectTimeout

      public RestTemplateBuilder setConnectTimeout(Duration connectTimeout)
      Sets the connection timeout on the underlying ClientHttpRequestFactory.
      Parameters:
      connectTimeout - the connection timeout
      Returns:
      a new builder instance.
      Since:
      2.1.0
    • setReadTimeout

      public RestTemplateBuilder setReadTimeout(Duration readTimeout)
      Sets the read timeout on the underlying ClientHttpRequestFactory.
      Parameters:
      readTimeout - the read timeout
      Returns:
      a new builder instance.
      Since:
      2.1.0
    • setBufferRequestBody

      public RestTemplateBuilder setBufferRequestBody(boolean bufferRequestBody)
      Sets if the underlying ClientHttpRequestFactory should buffer the request body internally.
      Parameters:
      bufferRequestBody - value of the bufferRequestBody parameter
      Returns:
      a new builder instance.
      Since:
      2.2.0
      See Also:
      • SimpleClientHttpRequestFactory.setBufferRequestBody(boolean)
      • HttpComponentsClientHttpRequestFactory.setBufferRequestBody(boolean)
    • customizers

      public RestTemplateBuilder customizers(RestTemplateCustomizer... customizers)
      Set the 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.
      Parameters:
      customizers - the customizers to set
      Returns:
      a new builder instance
      See Also:
    • customizers

      public RestTemplateBuilder customizers(Collection<? extends RestTemplateCustomizer> customizers)
      Set the 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.
      Parameters:
      customizers - the customizers to set
      Returns:
      a new builder instance
      See Also:
    • additionalCustomizers

      public RestTemplateBuilder additionalCustomizers(RestTemplateCustomizer... customizers)
      Add RestTemplateCustomizers that should be applied to the RestTemplate. Customizers are applied in the order that they were added after builder configuration has been applied.
      Parameters:
      customizers - the customizers to add
      Returns:
      a new builder instance
      See Also:
    • additionalCustomizers

      public RestTemplateBuilder additionalCustomizers(Collection<? extends RestTemplateCustomizer> customizers)
      Add RestTemplateCustomizers that should be applied to the RestTemplate. Customizers are applied in the order that they were added after builder configuration has been applied.
      Parameters:
      customizers - the customizers to add
      Returns:
      a new builder instance
      See Also:
    • requestCustomizers

      public RestTemplateBuilder requestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers)
      Set the 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.
      Parameters:
      requestCustomizers - the request customizers to set
      Returns:
      a new builder instance
      Since:
      2.2.0
      See Also:
    • requestCustomizers

      public RestTemplateBuilder requestCustomizers(Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers)
      Set the 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.
      Parameters:
      requestCustomizers - the request customizers to set
      Returns:
      a new builder instance
      Since:
      2.2.0
      See Also:
    • additionalRequestCustomizers

      public RestTemplateBuilder additionalRequestCustomizers(RestTemplateRequestCustomizer<?>... requestCustomizers)
      Add the RestTemplateRequestCustomizers that should be applied to the ClientHttpRequest. Customizers are applied in the order that they were added.
      Parameters:
      requestCustomizers - the request customizers to add
      Returns:
      a new builder instance
      Since:
      2.2.0
      See Also:
    • additionalRequestCustomizers

      public RestTemplateBuilder additionalRequestCustomizers(Collection<? extends RestTemplateRequestCustomizer<?>> requestCustomizers)
      Add the RestTemplateRequestCustomizers that should be applied to the ClientHttpRequest. Customizers are applied in the order that they were added.
      Parameters:
      requestCustomizers - the request customizers to add
      Returns:
      a new builder instance
      Since:
      2.2.0
      See Also:
    • build

      public org.springframework.web.client.RestTemplate build()
      Build a new RestTemplate instance and configure it using this builder.
      Returns:
      a configured RestTemplate instance.
      See Also:
    • 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.
      Type Parameters:
      T - the type of rest template
      Parameters:
      restTemplateClass - the template type to create
      Returns:
      a configured RestTemplate instance.
      See Also:
    • configure

      public <T extends org.springframework.web.client.RestTemplate> T configure(T restTemplate)
      Configure the provided RestTemplate instance using this builder.
      Type Parameters:
      T - the type of rest template
      Parameters:
      restTemplate - the RestTemplate to configure
      Returns:
      the rest template instance
      See Also:
    • buildRequestFactory

      public org.springframework.http.client.ClientHttpRequestFactory buildRequestFactory()
      Build a new ClientHttpRequestFactory instance using the settings of this builder.
      Returns:
      a ClientHttpRequestFactory or null
      Since:
      2.2.0