java.lang.Object
org.springframework.http.client.support.HttpAccessor
org.springframework.http.client.support.InterceptingHttpAccessor
org.springframework.web.client.RestTemplate
extends InterceptingHttpAccessor implements RestOperations
Synchronous client to perform HTTP requests, exposing a simple, template method API over underlying HTTP client libraries such as the JDK HttpURLConnection , Apache HttpComponents, and others. RestTemplate offers templates for common scenarios by HTTP method, in addition to the generalized exchange and execute methods that support less frequent cases.

RestTemplate is typically used as a shared component. However, its configuration does not support concurrent modification, and as such its configuration is typically prepared on startup. If necessary, you can create multiple, differently configured RestTemplate instances on startup. Such instances may use the same underlying ClientHttpRequestFactory if they need to share HTTP client resources.

NOTE: As of 5.0 this class is in maintenance mode, with only minor requests for changes and bugs to be accepted going forward. Please, consider using the org.springframework.web.reactive.client.WebClient which has a more modern API and supports sync, async, and streaming scenarios.

Since:
Author:
Arjen Poutsma, Brian Clozel, Roy Clarkson, Juergen Hoeller, Sam Brannen, Sebastien Deleuze
See Also:
  • HttpMessageConverter
  • RequestCallback
  • ResponseExtractor
  • ResponseErrorHandler
  • Field Summary

    Fields inherited from class org.springframework.http.client.support. HttpAccessor

    logger
    Create a new instance of the RestTemplate using default settings.
    Create a new instance of the RestTemplate using the given list of HttpMessageConverter to use.
    Create a new instance of the RestTemplate based on the given ClientHttpRequestFactory .
    Return a RequestCallback that sets the request Accept header based on the given response type, cross-checked against the configured message converters.
    delete ( String url, Object ... uriVariables)
    Delete the resources at the specified URI.
    delete ( String url, Map < String , ?> uriVariables)
    Delete the resources at the specified URI.
    Delete the resources at the specified URL.
    protected <T> T
    doExecute ( URI url, String uriTemplate, HttpMethod method, RequestCallback requestCallback, ResponseExtractor <T> responseExtractor)
    Execute the given method on the provided URI.
    protected <T> T
    doExecute ( URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor <T> responseExtractor)
    Deprecated.
    exchange ( String url, HttpMethod method, HttpEntity <?> requestEntity, Class <T> responseType, Object ... uriVariables)
    Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity .
    exchange ( String url, HttpMethod method, HttpEntity <?> requestEntity, Class <T> responseType, Map < String , ?> uriVariables)
    Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity .
    exchange ( String url, HttpMethod method, HttpEntity <?> requestEntity, ParameterizedTypeReference <T> responseType, Object ... uriVariables)
    Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity .
    exchange ( String url, HttpMethod method, HttpEntity <?> requestEntity, ParameterizedTypeReference <T> responseType, Map < String , ?> uriVariables)
    Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity .
    exchange ( URI url, HttpMethod method, HttpEntity <?> requestEntity, Class <T> responseType)
    Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity .
    exchange ( URI url, HttpMethod method, HttpEntity <?> requestEntity, ParameterizedTypeReference <T> responseType)
    Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity .
    exchange ( RequestEntity <?> entity, Class <T> responseType)
    Execute the request specified in the given RequestEntity and return the response as ResponseEntity .
    Execute the request specified in the given RequestEntity and return the response as ResponseEntity .
    execute ( String uriTemplate, HttpMethod method, RequestCallback requestCallback, ResponseExtractor <T> responseExtractor, Object ... uriVariables)
    Execute the HTTP method to the given URI template, preparing the request with the RequestCallback , and reading the response with a ResponseExtractor .
    execute ( String uriTemplate, HttpMethod method, RequestCallback requestCallback, ResponseExtractor <T> responseExtractor, Map < String , ?> uriVariables)
    Execute the HTTP method to the given URI template, preparing the request with the RequestCallback , and reading the response with a ResponseExtractor .
    execute ( URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor <T> responseExtractor)
    Execute the HTTP method to the given URL, preparing the request with the RequestCallback , and reading the response with a ResponseExtractor .
    Return the error handler.
    getForEntity ( String url, Class <T> responseType, Object ... uriVariables)
    Retrieve an entity by doing a GET on the specified URL.
    getForEntity ( String url, Class <T> responseType, Map < String , ?> uriVariables)
    Retrieve a representation by doing a GET on the URI template.
    getForEntity ( URI url, Class <T> responseType)
    Retrieve a representation by doing a GET on the URL.
    getForObject ( String url, Class <T> responseType, Object ... uriVariables)
    Retrieve a representation by doing a GET on the specified URL.
    getForObject ( String url, Class <T> responseType, Map < String , ?> uriVariables)
    Retrieve a representation by doing a GET on the URI template.
    getForObject ( URI url, Class <T> responseType)
    Retrieve a representation by doing a GET on the URL.
    Return the list of message body converters.
    Return the configured URI template handler.
    protected void
    Handle the given response, performing appropriate logging and invoking the ResponseErrorHandler if necessary.
    Return a response extractor for HttpHeaders .
    headForHeaders ( String url, Object ... uriVariables)
    Retrieve all headers of the resource specified by the URI template.
    headForHeaders ( String url, Map < String , ?> uriVariables)
    Retrieve all headers of the resource specified by the URI template.
    Retrieve all headers of the resource specified by the URL.
    Return a RequestCallback implementation that writes the given object to the request stream.
    httpEntityCallback ( Object requestBody, Type responseType)
    Return a RequestCallback implementation that: Sets the request Accept header based on the given response type, cross-checked against the configured message converters.
    optionsForAllow ( String url, Object ... uriVariables)
    Return the value of the Allow header for the given URI.
    optionsForAllow ( String url, Map < String , ?> uriVariables)
    Return the value of the Allow header for the given URI.
    Return the value of the Allow header for the given URL.
    patchForObject ( String url, Object request, Class <T> responseType, Object ... uriVariables)
    Update a resource by PATCHing the given object to the URI template, and return the representation found in the response.
    patchForObject ( String url, Object request, Class <T> responseType, Map < String , ?> uriVariables)
    Update a resource by PATCHing the given object to the URI template, and return the representation found in the response.
    patchForObject ( URI url, Object request, Class <T> responseType)
    Update a resource by PATCHing the given object to the URL, and return the representation found in the response.
    <T> ResponseEntity <T>
    postForEntity ( String url, Object request, Class <T> responseType, Object ... uriVariables)
    Create a new resource by POSTing the given object to the URI template, and returns the response as ResponseEntity .
    postForEntity ( String url, Object request, Class <T> responseType, Map < String , ?> uriVariables)
    Create a new resource by POSTing the given object to the URI template, and returns the response as HttpEntity .
    postForEntity ( URI url, Object request, Class <T> responseType)
    Create a new resource by POSTing the given object to the URL, and returns the response as ResponseEntity .
    postForLocation ( String url, Object request, Object ... uriVariables)
    Create a new resource by POSTing the given object to the URI template, and returns the value of the Location header.
    postForLocation ( String url, Object request, Map < String , ?> uriVariables)
    Create a new resource by POSTing the given object to the URI template, and returns the value of the Location header.
    Create a new resource by POSTing the given object to the URL, and returns the value of the Location header.
    postForObject ( String url, Object request, Class <T> responseType, Object ... uriVariables)
    Create a new resource by POSTing the given object to the URI template, and returns the representation found in the response.
    postForObject ( String url, Object request, Class <T> responseType, Map < String , ?> uriVariables)
    Create a new resource by POSTing the given object to the URI template, and returns the representation found in the response.
    postForObject ( URI url, Object request, Class <T> responseType)
    Create a new resource by POSTing the given object to the URL, and returns the representation found in the response.
    put ( String url, Object request, Object ... uriVariables)
    Create or update a resource by PUTting the given object to the URI.
    put ( String url, Object request, Map < String , ?> uriVariables)
    Creates a new resource by PUTting the given object to URI template.
    put ( URI url, Object request)
    Creates a new resource by PUTting the given object to URL.
    Return a ResponseExtractor that prepares a ResponseEntity .
    Configure default URI variable values.
    Set the error handler.
    Set the message body converters to use.
    setObservationConvention ( ClientRequestObservationConvention observationConvention)
    Configure an ObservationConvention that sets the name of the observation as well as its KeyValues extracted from the ClientRequestObservationContext .
    setObservationRegistry (io.micrometer.observation.ObservationRegistry observationRegistry)
    Configure an ObservationRegistry for collecting spans and metrics for request execution.
    Configure a strategy for expanding URI templates.

    Methods inherited from class org.springframework.http.client.support. InterceptingHttpAccessor

    getInterceptors , getRequestFactory , setInterceptors , setRequestFactory

    Methods inherited from class org.springframework.http.client.support. HttpAccessor

    createRequest , getClientHttpRequestInitializers , setClientHttpRequestInitializers

    Methods inherited from class java.lang. Object

    clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait

    RestTemplate

    public RestTemplate ()
    Create a new instance of the RestTemplate using default settings. Default HttpMessageConverters are initialized.

    RestTemplate

    public RestTemplate ( ClientHttpRequestFactory requestFactory)
    Create a new instance of the RestTemplate based on the given ClientHttpRequestFactory .
    Parameters:
    requestFactory - the HTTP request factory to use
    See Also:
  • SimpleClientHttpRequestFactory
  • HttpComponentsClientHttpRequestFactory
  • RestTemplate

    public RestTemplate ( List < HttpMessageConverter <?>> messageConverters)
    Create a new instance of the RestTemplate using the given list of HttpMessageConverter to use.
    Parameters:
    messageConverters - the list of HttpMessageConverter to use
    Since:
    3.2.7

    setMessageConverters

    public void setMessageConverters ( List < HttpMessageConverter <?>> messageConverters)
    Set the message body converters to use.

    These converters are used to convert from and to HTTP requests and responses.

    getMessageConverters

    public List < HttpMessageConverter <?>> getMessageConverters ()
    Return the list of message body converters.

    The returned List is active and may get appended to.

    setErrorHandler

    public void setErrorHandler ( ResponseErrorHandler errorHandler)
    Set the error handler.

    By default, RestTemplate uses a DefaultResponseErrorHandler .

    setDefaultUriVariables

    public void setDefaultUriVariables ( Map < String , ?> uriVars)
    Configure default URI variable values. This is a shortcut for:
     DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory();
     handler.setDefaultUriVariables(...);
     RestTemplate restTemplate = new RestTemplate();
     restTemplate.setUriTemplateHandler(handler);
    
    Parameters:
    uriVars - the default URI variable values
    Since:

    setUriTemplateHandler

    public void setUriTemplateHandler(UriTemplateHandler handler)
    Configure a strategy for expanding URI templates.

    By default, DefaultUriBuilderFactory is used and for backwards compatibility, the encoding mode is set to URI_COMPONENT. As of 5.0.8, prefer using TEMPLATE_AND_VALUES.

    Parameters:
    handler - the URI template handler to use

    setObservationRegistry

    public void setObservationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry)
    Configure an ObservationRegistry for collecting spans and metrics for request execution. By default, observations are no-ops.
    Parameters:
    observationRegistry - the observation registry to use
    Since:

    setObservationConvention

    public void setObservationConvention(ClientRequestObservationConvention observationConvention)
    Configure an ObservationConvention that sets the name of the observation as well as its KeyValues extracted from the ClientRequestObservationContext. If none set, the default convention will be used.
    Parameters:
    observationConvention - the observation convention to use
    Since:
    See Also:
  • setObservationRegistry(ObservationRegistry)
  • public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Retrieve a representation by doing a GET on the specified URL. The response (if any) is converted and returned.

    URI Template variables are expanded using the given URI variables, if any.

    Specified by:
    getForObject in interface RestOperations
    Parameters:
    url - the URL
    responseType - the type of the return value
    uriVariables - the variables to expand the template
    Returns:
    the converted object
    Throws:
    RestClientException
    public <T> T getForObject ( String url, Class <T> responseType, Map < String , ?> uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Retrieve a representation by doing a GET on the URI template. The response (if any) is converted and returned.

    URI Template variables are expanded using the given map.

    Specified by:
    getForObject in interface RestOperations
    Parameters:
    url - the URL
    responseType - the type of the return value
    uriVariables - the map containing variables for the URI template
    Returns:
    the converted object
    Throws:
    RestClientException
    public <T> T getForObject ( URI url, Class <T> responseType) throws RestClientException
    Description copied from interface: RestOperations
    Retrieve a representation by doing a GET on the URL. The response (if any) is converted and returned.
    Specified by:
    getForObject in interface RestOperations
    Parameters:
    url - the URL
    responseType - the type of the return value
    Returns:
    the converted object
    Throws:
    RestClientException

    getForEntity

    public <T> ResponseEntity <T> getForEntity ( String url, Class <T> responseType, Object ... uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Retrieve an entity by doing a GET on the specified URL. The response is converted and stored in a ResponseEntity .

    URI Template variables are expanded using the given URI variables, if any.

    Specified by:
    getForEntity in interface RestOperations
    Parameters:
    url - the URL
    responseType - the type of the return value
    uriVariables - the variables to expand the template
    Returns:
    the entity
    Throws:
    RestClientException

    getForEntity

    public <T> ResponseEntity <T> getForEntity ( String url, Class <T> responseType, Map < String , ?> uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Retrieve a representation by doing a GET on the URI template. The response is converted and stored in a ResponseEntity .

    URI Template variables are expanded using the given map.

    Specified by:
    getForEntity in interface RestOperations
    Parameters:
    url - the URL
    responseType - the type of the return value
    uriVariables - the map containing variables for the URI template
    Returns:
    the converted object
    Throws:
    RestClientException

    getForEntity

    public <T> ResponseEntity <T> getForEntity ( URI url, Class <T> responseType) throws RestClientException
    Description copied from interface: RestOperations
    Retrieve a representation by doing a GET on the URL. The response is converted and stored in a ResponseEntity .
    Specified by:
    getForEntity in interface RestOperations
    Parameters:
    url - the URL
    responseType - the type of the return value
    Returns:
    the converted object
    Throws:
    RestClientException

    headForHeaders

    public HttpHeaders headForHeaders ( String url, Object ... uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Retrieve all headers of the resource specified by the URI template.

    URI Template variables are expanded using the given URI variables, if any.

    Specified by:
    headForHeaders in interface RestOperations
    Parameters:
    url - the URL
    uriVariables - the variables to expand the template
    Returns:
    all HTTP headers of that resource
    Throws:
    RestClientException

    headForHeaders

    public HttpHeaders headForHeaders ( String url, Map < String , ?> uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Retrieve all headers of the resource specified by the URI template.

    URI Template variables are expanded using the given map.

    Specified by:
    headForHeaders in interface RestOperations
    Parameters:
    url - the URL
    uriVariables - the map containing variables for the URI template
    Returns:
    all HTTP headers of that resource
    Throws:
    RestClientException

    headForHeaders

    public HttpHeaders headForHeaders ( URI url) throws RestClientException
    Description copied from interface: RestOperations
    Retrieve all headers of the resource specified by the URL.
    Specified by:
    headForHeaders in interface RestOperations
    Parameters:
    url - the URL
    Returns:
    all HTTP headers of that resource
    Throws:
    RestClientException
    Object ... uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Create a new resource by POSTing the given object to the URI template, and returns the value of the Location header. This header typically indicates where the new resource is stored.

    URI Template variables are expanded using the given URI variables, if any.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    The body of the entity, or request itself, can be a MultiValueMap to create a multipart request. The values in the MultiValueMap can be any Object representing the body of the part, or an HttpEntity representing a part with body and headers.

    Specified by:
    postForLocation in interface RestOperations
    Parameters:
    url - the URL
    request - the Object to be POSTed (may be null )
    uriVariables - the variables to expand the template
    Returns:
    the value for the Location header
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • Map < String , ?> uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Create a new resource by POSTing the given object to the URI template, and returns the value of the Location header. This header typically indicates where the new resource is stored.

    URI Template variables are expanded using the given map.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request

    The body of the entity, or request itself, can be a MultiValueMap to create a multipart request. The values in the MultiValueMap can be any Object representing the body of the part, or an HttpEntity representing a part with body and headers.

    Specified by:
    postForLocation in interface RestOperations
    Parameters:
    url - the URL
    request - the Object to be POSTed (may be null )
    uriVariables - the variables to expand the template
    Returns:
    the value for the Location header
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • Object request) throws RestClientException
    Description copied from interface: RestOperations
    Create a new resource by POSTing the given object to the URL, and returns the value of the Location header. This header typically indicates where the new resource is stored.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    The body of the entity, or request itself, can be a MultiValueMap to create a multipart request. The values in the MultiValueMap can be any Object representing the body of the part, or an HttpEntity representing a part with body and headers.

    Specified by:
    postForLocation in interface RestOperations
    Parameters:
    url - the URL
    request - the Object to be POSTed (may be null )
    Returns:
    the value for the Location header
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • Object ... uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Create a new resource by POSTing the given object to the URI template, and returns the representation found in the response.

    URI Template variables are expanded using the given URI variables, if any.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    The body of the entity, or request itself, can be a MultiValueMap to create a multipart request. The values in the MultiValueMap can be any Object representing the body of the part, or an HttpEntity representing a part with body and headers.

    Specified by:
    postForObject in interface RestOperations
    Parameters:
    url - the URL
    request - the Object to be POSTed (may be null )
    responseType - the type of the return value
    uriVariables - the variables to expand the template
    Returns:
    the converted object
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • Map < String , ?> uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Create a new resource by POSTing the given object to the URI template, and returns the representation found in the response.

    URI Template variables are expanded using the given map.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    The body of the entity, or request itself, can be a MultiValueMap to create a multipart request. The values in the MultiValueMap can be any Object representing the body of the part, or an HttpEntity representing a part with body and headers.

    Specified by:
    postForObject in interface RestOperations
    Parameters:
    url - the URL
    request - the Object to be POSTed (may be null )
    responseType - the type of the return value
    uriVariables - the variables to expand the template
    Returns:
    the converted object
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • Class <T> responseType) throws RestClientException
    Description copied from interface: RestOperations
    Create a new resource by POSTing the given object to the URL, and returns the representation found in the response.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    The body of the entity, or request itself, can be a MultiValueMap to create a multipart request. The values in the MultiValueMap can be any Object representing the body of the part, or an HttpEntity representing a part with body and headers.

    Specified by:
    postForObject in interface RestOperations
    Parameters:
    url - the URL
    request - the Object to be POSTed (may be null )
    responseType - the type of the return value
    Returns:
    the converted object
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • Object ... uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Create a new resource by POSTing the given object to the URI template, and returns the response as ResponseEntity .

    URI Template variables are expanded using the given URI variables, if any.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    The body of the entity, or request itself, can be a MultiValueMap to create a multipart request. The values in the MultiValueMap can be any Object representing the body of the part, or an HttpEntity representing a part with body and headers.

    Specified by:
    postForEntity in interface RestOperations
    Parameters:
    url - the URL
    request - the Object to be POSTed (may be null )
    uriVariables - the variables to expand the template
    Returns:
    the converted object
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • Map < String , ?> uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Create a new resource by POSTing the given object to the URI template, and returns the response as HttpEntity .

    URI Template variables are expanded using the given map.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    The body of the entity, or request itself, can be a MultiValueMap to create a multipart request. The values in the MultiValueMap can be any Object representing the body of the part, or an HttpEntity representing a part with body and headers.

    Specified by:
    postForEntity in interface RestOperations
    Parameters:
    url - the URL
    request - the Object to be POSTed (may be null )
    uriVariables - the variables to expand the template
    Returns:
    the converted object
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • Class <T> responseType) throws RestClientException
    Description copied from interface: RestOperations
    Create a new resource by POSTing the given object to the URL, and returns the response as ResponseEntity .

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    The body of the entity, or request itself, can be a MultiValueMap to create a multipart request. The values in the MultiValueMap can be any Object representing the body of the part, or an HttpEntity representing a part with body and headers.

    Specified by:
    postForEntity in interface RestOperations
    Parameters:
    url - the URL
    request - the Object to be POSTed (may be null )
    Returns:
    the converted object
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • Object ... uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Create or update a resource by PUTting the given object to the URI.

    URI Template variables are expanded using the given URI variables, if any.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    Specified by:
    put in interface RestOperations
    Parameters:
    url - the URL
    request - the Object to be PUT (may be null )
    uriVariables - the variables to expand the template
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • Map < String , ?> uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Creates a new resource by PUTting the given object to URI template.

    URI Template variables are expanded using the given map.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    Specified by:
    put in interface RestOperations
    Parameters:
    url - the URL
    request - the Object to be PUT (may be null )
    uriVariables - the variables to expand the template
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • Object request) throws RestClientException
    Description copied from interface: RestOperations
    Creates a new resource by PUTting the given object to URL.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    Specified by:
    put in interface RestOperations
    Parameters:
    url - the URL
    request - the Object to be PUT (may be null )
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • Object ... uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Update a resource by PATCHing the given object to the URI template, and return the representation found in the response.

    URI Template variables are expanded using the given URI variables, if any.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    NOTE: The standard JDK HTTP library does not support HTTP PATCH. You need to use the Apache HttpComponents or OkHttp request factory.

    Specified by:
    patchForObject in interface RestOperations
    Parameters:
    url - the URL
    request - the object to be PATCHed (may be null )
    responseType - the type of the return value
    uriVariables - the variables to expand the template
    Returns:
    the converted object
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • InterceptingHttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
  • org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory
  • OkHttp3ClientHttpRequestFactory
  • Map < String , ?> uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Update a resource by PATCHing the given object to the URI template, and return the representation found in the response.

    URI Template variables are expanded using the given map.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    NOTE: The standard JDK HTTP library does not support HTTP PATCH. You need to use the Apache HttpComponents or OkHttp request factory.

    Specified by:
    patchForObject in interface RestOperations
    Parameters:
    url - the URL
    request - the object to be PATCHed (may be null )
    responseType - the type of the return value
    uriVariables - the variables to expand the template
    Returns:
    the converted object
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • InterceptingHttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
  • org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory
  • OkHttp3ClientHttpRequestFactory
  • Class <T> responseType) throws RestClientException
    Description copied from interface: RestOperations
    Update a resource by PATCHing the given object to the URL, and return the representation found in the response.

    The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.

    NOTE: The standard JDK HTTP library does not support HTTP PATCH. You need to use the Apache HttpComponents or OkHttp request factory.

    Specified by:
    patchForObject in interface RestOperations
    Parameters:
    url - the URL
    request - the object to be PATCHed (may be null )
    responseType - the type of the return value
    Returns:
    the converted object
    Throws:
    RestClientException
    See Also:
  • HttpEntity
  • InterceptingHttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
  • org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory
  • OkHttp3ClientHttpRequestFactory
  • Object ... uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Delete the resources at the specified URI.

    URI Template variables are expanded using the given URI variables, if any.

    Specified by:
    delete in interface RestOperations
    Parameters:
    url - the URL
    uriVariables - the variables to expand in the template
    Throws:
    RestClientException
    public void delete ( String url, Map < String , ?> uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Delete the resources at the specified URI.

    URI Template variables are expanded using the given map.

    Specified by:
    delete in interface RestOperations
    Parameters:
    url - the URL
    uriVariables - the variables to expand the template
    Throws:
    RestClientException
    public void delete ( URI url) throws RestClientException
    Description copied from interface: RestOperations
    Delete the resources at the specified URL.
    Specified by:
    delete in interface RestOperations
    Parameters:
    url - the URL
    Throws:
    RestClientException

    optionsForAllow

    public Set < HttpMethod > optionsForAllow ( String url, Object ... uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Return the value of the Allow header for the given URI.

    URI Template variables are expanded using the given URI variables, if any.

    Specified by:
    optionsForAllow in interface RestOperations
    Parameters:
    url - the URL
    uriVariables - the variables to expand in the template
    Returns:
    the value of the Allow header
    Throws:
    RestClientException

    optionsForAllow

    public Set < HttpMethod > optionsForAllow ( String url, Map < String , ?> uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Return the value of the Allow header for the given URI.

    URI Template variables are expanded using the given map.

    Specified by:
    optionsForAllow in interface RestOperations
    Parameters:
    url - the URL
    uriVariables - the variables to expand in the template
    Returns:
    the value of the Allow header
    Throws:
    RestClientException

    optionsForAllow

    public Set < HttpMethod > optionsForAllow ( URI url) throws RestClientException
    Description copied from interface: RestOperations
    Return the value of the Allow header for the given URL.
    Specified by:
    optionsForAllow in interface RestOperations
    Parameters:
    url - the URL
    Returns:
    the value of the Allow header
    Throws:
    RestClientException
    Object ... uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity .

    URI Template variables are expanded using the given URI variables, if any.

    Specified by:
    exchange in interface RestOperations
    Parameters:
    url - the URL
    method - the HTTP method (GET, POST, etc)
    requestEntity - the entity (headers and/or body) to write to the request may be null )
    responseType - the type to convert the response to, or Void.class for no body
    uriVariables - the variables to expand in the template
    Returns:
    the response as entity
    Throws:
    RestClientException
    Map < String , ?> uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity .

    URI Template variables are expanded using the given URI variables, if any.

    Specified by:
    exchange in interface RestOperations
    Parameters:
    url - the URL
    method - the HTTP method (GET, POST, etc)
    requestEntity - the entity (headers and/or body) to write to the request (may be null )
    responseType - the type to convert the response to, or Void.class for no body
    uriVariables - the variables to expand in the template
    Returns:
    the response as entity
    Throws:
    RestClientException
    Class <T> responseType) throws RestClientException
    Description copied from interface: RestOperations
    Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity .
    Specified by:
    exchange in interface RestOperations
    Parameters:
    url - the URL
    method - the HTTP method (GET, POST, etc)
    requestEntity - the entity (headers and/or body) to write to the request (may be null )
    responseType - the type to convert the response to, or Void.class for no body
    Returns:
    the response as entity
    Throws:
    RestClientException
    HttpEntity <?> requestEntity, ParameterizedTypeReference <T> responseType, Object ... uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity . The given ParameterizedTypeReference is used to pass generic type information:
     ParameterizedTypeReference<List<MyBean>> myBean =
         new ParameterizedTypeReference<List<MyBean>>() {};
     ResponseEntity<List<MyBean>> response =
         template.exchange("https://example.com",HttpMethod.GET, null, myBean);
    
    Specified by:
    exchange in interface RestOperations
    Parameters:
    url - the URL
    method - the HTTP method (GET, POST, etc)
    requestEntity - the entity (headers and/or body) to write to the request (may be null)
    responseType - the type to convert the response to, or Void.class for no body
    uriVariables - the variables to expand in the template
    Returns:
    the response as entity
    Throws:
    RestClientException
    HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String,?> uriVariables) throws RestClientException
    Description copied from interface: RestOperations
    Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity . The given ParameterizedTypeReference is used to pass generic type information:
     ParameterizedTypeReference<List<MyBean>> myBean =
         new ParameterizedTypeReference<List<MyBean>>() {};
     ResponseEntity<List<MyBean>> response =
         template.exchange("https://example.com",HttpMethod.GET, null, myBean);
    
    Specified by:
    exchange in interface RestOperations
    Parameters:
    url - the URL
    method - the HTTP method (GET, POST, etc)
    requestEntity - the entity (headers and/or body) to write to the request (may be null)
    responseType - the type to convert the response to, or Void.class for no body
    uriVariables - the variables to expand in the template
    Returns:
    the response as entity
    Throws:
    RestClientException
    HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException
    Description copied from interface: RestOperations
    Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity . The given ParameterizedTypeReference is used to pass generic type information:
     ParameterizedTypeReference<List<MyBean>> myBean =
         new ParameterizedTypeReference<List<MyBean>>() {};
     ResponseEntity<List<MyBean>> response =
         template.exchange("https://example.com",HttpMethod.GET, null, myBean);
    
    Specified by:
    exchange in interface RestOperations
    Parameters:
    url - the URL
    method - the HTTP method (GET, POST, etc)
    requestEntity - the entity (headers and/or body) to write to the request (may be null)
    responseType - the type to convert the response to, or Void.class for no body
    Returns:
    the response as entity
    Throws:
    RestClientException

    exchange

    public <T> ResponseEntity<T> exchange(RequestEntity<?> entity, Class<T> responseType) throws RestClientException
    Description copied from interface: RestOperations
    Execute the request specified in the given RequestEntity and return the response as ResponseEntity. Typically used in combination with the static builder methods on RequestEntity, for instance:
     MyRequest body = ...
     RequestEntity request = RequestEntity
         .post(URI.create("https://example.com/foo"))
         .accept(MediaType.APPLICATION_JSON)
         .body(body);
     ResponseEntity<MyResponse> response = template.exchange(request, MyResponse.class);
    
    Specified by:
    exchange in interface RestOperations
    Parameters:
    entity - the entity to write to the request
    responseType - the type to convert the response to, or Void.class for no body
    Returns:
    the response as entity
    Throws:
    RestClientException

    exchange

    public <T> ResponseEntity<T> exchange(RequestEntity<?> entity, ParameterizedTypeReference<T> responseType) throws RestClientException
    Description copied from interface: RestOperations
    Execute the request specified in the given RequestEntity and return the response as ResponseEntity. The given ParameterizedTypeReference is used to pass generic type information:
     MyRequest body = ...
     RequestEntity request = RequestEntity
         .post(URI.create("https://example.com/foo"))
         .accept(MediaType.APPLICATION_JSON)
         .body(body);
     ParameterizedTypeReference<List<MyResponse>> myBean =
         new ParameterizedTypeReference<List<MyResponse>>() {};
     ResponseEntity<List<MyResponse>> response = template.exchange(request, myBean);
    
    Specified by:
    exchange in interface RestOperations
    Parameters:
    entity - the entity to write to the request
    responseType - the type to convert the response to, or Void.class for no body
    Returns:
    the response as entity
    Throws:
    RestClientException
    ResponseExtractor<T> responseExtractor, Object... uriVariables) throws RestClientException
    Execute the HTTP method to the given URI template, preparing the request with the RequestCallback, and reading the response with a ResponseExtractor.

    URI Template variables are expanded using the given URI variables, if any.

    To provide a RequestCallback or ResponseExtractor only, but not both, consider using:

  • acceptHeaderRequestCallback(Class)
  • httpEntityCallback(Object)
  • httpEntityCallback(Object, Type)
  • responseEntityExtractor(Type)
    Specified by:
    execute in interface RestOperations
    Parameters:
    uriTemplate - the URI template
    method - the HTTP method (GET, POST, etc)
    requestCallback - object that prepares the request
    responseExtractor - object that extracts the return value from the response
    uriVariables - the variables to expand in the template
    Returns:
    an arbitrary object, as returned by the ResponseExtractor
    Throws:
    RestClientException
    ResponseExtractor<T> responseExtractor, Map<String,?> uriVariables) throws RestClientException
  • Execute the HTTP method to the given URI template, preparing the request with the RequestCallback, and reading the response with a ResponseExtractor.

    URI Template variables are expanded using the given URI variables map.

    To provide a RequestCallback or ResponseExtractor only, but not both, consider using:

  • acceptHeaderRequestCallback(Class)
  • httpEntityCallback(Object)
  • httpEntityCallback(Object, Type)
  • responseEntityExtractor(Type)
    Specified by:
    execute in interface RestOperations
    Parameters:
    uriTemplate - the URI template
    method - the HTTP method (GET, POST, etc)
    requestCallback - object that prepares the request
    responseExtractor - object that extracts the return value from the response
    uriVariables - the variables to expand in the template
    Returns:
    an arbitrary object, as returned by the ResponseExtractor
    Throws:
    RestClientException
    ResponseExtractor<T> responseExtractor) throws RestClientException
  • Execute the HTTP method to the given URL, preparing the request with the RequestCallback, and reading the response with a ResponseExtractor.

    To provide a RequestCallback or ResponseExtractor only, but not both, consider using:

  • acceptHeaderRequestCallback(Class)
  • httpEntityCallback(Object)
  • httpEntityCallback(Object, Type)
  • responseEntityExtractor(Type)
    Specified by:
    execute in interface RestOperations
    Parameters:
    url - the URL
    method - the HTTP method (GET, POST, etc)
    requestCallback - object that prepares the request
    responseExtractor - object that extracts the return value from the response
    Returns:
    an arbitrary object, as returned by the ResponseExtractor
    Throws:
    RestClientException
    throws RestClientException
  • Deprecated.
    Execute the given method on the provided URI.

    The ClientHttpRequest is processed using the RequestCallback; the response with the ResponseExtractor.

    Parameters:
    url - the fully-expanded URL to connect to
    method - the HTTP method to execute (GET, POST, etc.)
    requestCallback - object that prepares the request (can be null)
    responseExtractor - object that extracts the return value from the response (can be null)
    Returns:
    an arbitrary object, as returned by the ResponseExtractor
    Throws:
    RestClientException
    ResponseExtractor<T> responseExtractor) throws RestClientException
    Execute the given method on the provided URI.

    The ClientHttpRequest is processed using the RequestCallback; the response with the ResponseExtractor.

    Parameters:
    url - the fully-expanded URL to connect to
    uriTemplate - the URI template that was used for creating the expanded URL
    method - the HTTP method to execute (GET, POST, etc.)
    requestCallback - object that prepares the request (can be null)
    responseExtractor - object that extracts the return value from the response (can be null)
    Returns:
    an arbitrary object, as returned by the ResponseExtractor
    Throws:
    RestClientException
    Since:
    protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException
    Handle the given response, performing appropriate logging and invoking the ResponseErrorHandler if necessary.

    Can be overridden in subclasses.

    Parameters:
    url - the fully-expanded URL to connect to
    method - the HTTP method to execute (GET, POST, etc.)
    response - the resulting ClientHttpResponse
    Throws:
    IOException - if propagated from ResponseErrorHandler
    Since:
    4.1.6
    See Also:
  • setErrorHandler(org.springframework.web.client.ResponseErrorHandler)
  • acceptHeaderRequestCallback

    public <T> RequestCallback acceptHeaderRequestCallback(Class<T> responseType)
    Return a RequestCallback that sets the request Accept header based on the given response type, cross-checked against the configured message converters.

    httpEntityCallback

    public <T> RequestCallback httpEntityCallback(@Nullable Object requestBody)
    Return a RequestCallback implementation that writes the given object to the request stream.

    httpEntityCallback

    public <T> RequestCallback httpEntityCallback(@Nullable Object requestBody, Type responseType)
    Return a RequestCallback implementation that:
  • Sets the request Accept header based on the given response type, cross-checked against the configured message converters.
  • Writes the given object to the request stream.

    responseEntityExtractor

    public <T> ResponseExtractor<ResponseEntity<T>> responseEntityExtractor(Type responseType)
    Return a ResponseExtractor that prepares a ResponseEntity.
  •