org.springframework.boot.web.client

Class 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
    Author:
    Stephane Nicoll, Phillip Webb, Andy Wilkinson
    • Method Detail

      • 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 '/'. 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.
        Parameters:
        messageConverters - the converters to set
        Returns:
        a new builder instance
        See Also:
        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.
        Parameters:
        messageConverters - the converters to set
        Returns:
        a new builder instance
        See Also:
        additionalMessageConverters(HttpMessageConverter...)
      • additionalMessageConverters

        public RestTemplateBuilder additionalMessageConverters(org.springframework.http.converter.HttpMessageConverter<?>... messageConverters)
        Add additional HttpMessageConverters that should be used with the RestTemplate.
        Parameters:
        messageConverters - the converters to add
        Returns:
        a new builder instance
        See Also:
        messageConverters(HttpMessageConverter...)
      • additionalMessageConverters

        public RestTemplateBuilder additionalMessageConverters(Collection<? extends org.springframework.http.converter.HttpMessageConverter<?>> messageConverters)
        Add additional HttpMessageConverters that should be used with the RestTemplate.
        Parameters:
        messageConverters - the converters to add
        Returns:
        a new builder instance
        See Also:
        messageConverters(HttpMessageConverter...)
      • 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:
        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.
        Parameters:
        interceptors - the interceptors to set
        Returns:
        a new builder instance
        Since:
        1.4.1
        See Also:
        additionalInterceptors(ClientHttpRequestInterceptor...)
      • 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(ClientHttpRequestInterceptor...)
      • 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:
        interceptors(ClientHttpRequestInterceptor...)
      • 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:
        interceptors(ClientHttpRequestInterceptor...)
      • requestFactory

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

        public RestTemplateBuilder requestFactory(org.springframework.http.client.ClientHttpRequestFactory requestFactory)
        Set the ClientHttpRequestFactory that should be used with the RestTemplate.
        Parameters:
        requestFactory - the request factory to use
        Returns:
        a new builder instance
      • 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
      • basicAuthorization

        public RestTemplateBuilder basicAuthorization(String username,
                                                      String password)
        Add HTTP basic authentication to requests. See BasicAuthorizationInterceptor for details.
        Parameters:
        username - the user name
        password - the password
        Returns:
        a new builder instance
      • setConnectTimeout

        public RestTemplateBuilder setConnectTimeout(int connectTimeout)
        Sets the connect timeout in milliseconds on the underlying ClientHttpRequestFactory.
        Parameters:
        connectTimeout - the connect timeout in milliseconds
        Returns:
        a new builder instance.
      • setReadTimeout

        public RestTemplateBuilder setReadTimeout(int readTimeout)
        Sets the read timeout in milliseconds on the underlying ClientHttpRequestFactory.
        Parameters:
        readTimeout - the read timeout in milliseconds
        Returns:
        a new builder instance.
      • 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(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.
        Type Parameters:
        T - the type of rest template
        Parameters:
        restTemplateClass - the template type to create
        Returns:
        a configured RestTemplate instance.
        See Also:
        build(), configure(RestTemplate)
      • 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:
        build(), build(Class)

Copyright © 2016 Pivotal Software, Inc.. All rights reserved.