public class

RestTemplate

extends InterceptingHttpAccessor
implements RestOperations
java.lang.Object
   ↳ org.springframework.http.client.support.HttpAccessor
     ↳ org.springframework.http.client.support.InterceptingHttpAccessor
       ↳ org.springframework.web.client.RestTemplate

Class Overview

The central class for client-side HTTP access. It simplifies communication with HTTP servers, and enforces RESTful principles. It handles HTTP connections, leaving application code to provide URLs (with possible template variables) and extract results.

The main entry points of this template are the methods named after the six main HTTP methods:

HTTP methodRestTemplate methods
DELETEdelete(String, Object...)
GETgetForObject(String, Class, Object...)
getForEntity(String, Class, Object...)
HEADheadForHeaders(String, Object...)
OPTIONSoptionsForAllow(String, Object...)
POSTpostForLocation(String, Object, Object...)
postForObject(String, Object, Class, Object...)
PUTput(String, Object, Object...)
anyexchange(String, HttpMethod, HttpEntity, Class, Object...)
execute(String, HttpMethod, RequestCallback, ResponseExtractor, Object...)

For each of these HTTP methods, there are three corresponding Java methods in the RestTemplate. Two variant take a String URI as first argument (eg. getForObject(String, Class, Object[]), getForObject(String, Class, Map)), and are capable of substituting any URI templates in that URL using either a String variable arguments array, or a Map<String, String>. The string varargs variant expands the given template variables in order, so that

 String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", String.class,"42",
 "21");
 
will perform a GET on http://example.com/hotels/42/bookings/21. The map variant expands the template based on variable name, and is therefore more useful when using many variables, or when a single variable is used multiple times. For example:
 Map<String, String> vars = Collections.singletonMap("hotel", "42");
 String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/rooms/{hotel}", String.class, vars);
 
will perform a GET on http://example.com/hotels/42/rooms/42. Alternatively, there are URI variant methods (getForObject(URI, Class)), which do not allow for URI templates, but allow you to reuse a single, expanded URI multiple times.

Furthermore, the String-argument methods assume that the URL String is unencoded. This means that

 restTemplate.getForObject("http://example.com/hotel list");
 
will perform a GET on http://example.com/hotel%20list. As a result, any URL passed that is already encoded will be encoded twice (i.e. http://example.com/hotel%20list will become http://example.com/hotel%2520list). If this behavior is undesirable, use the URI-argument methods, which will not perform any URL encoding.

Objects passed to and returned from these methods are converted to and from HTTP messages by HttpMessageConverter instances. Converters for the main mime types are registered by default, but you can also write your own converter and register it via the messageConverters bean property.

This template uses a SimpleClientHttpRequestFactory and a DefaultResponseErrorHandler as default strategies for creating HTTP connections or handling HTTP errors, respectively. These defaults can be overridden through the requestFactory and errorHandler bean properties.

Summary

[Expand]
Inherited Fields
From class org.springframework.http.client.support.HttpAccessor
Public Constructors
RestTemplate()
Create a new instance of the RestTemplate using default settings.
RestTemplate(ClientHttpRequestFactory requestFactory)
Create a new instance of the RestTemplate based on the given ClientHttpRequestFactory.
Public Methods
void delete(String url, Object... urlVariables)
Delete the resources at the specified URI.
void delete(URI url)
Delete the resources at the specified URL.
void delete(String url, Map<String, ?> urlVariables)
Delete the resources at the specified URI.
<T> ResponseEntity<T> 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.
<T> ResponseEntity<T> 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.
<T> ResponseEntity<T> 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.
<T> T 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.
<T> T execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Object... urlVariables)
Execute the HTTP method to the given URI template, preparing the request with the RequestCallback, and reading the response with a ResponseExtractor.
<T> T execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Map<String, ?> urlVariables)
Execute the HTTP method to the given URI template, preparing the request with the RequestCallback, and reading the response with a ResponseExtractor.
ResponseErrorHandler getErrorHandler()
Return the error handler.
<T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType)
Retrieve a representation by doing a GET on the URL .
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> urlVariables)
Retrieve a representation by doing a GET on the URI template.
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... urlVariables)
Retrieve an entity by doing a GET on the specified URL.
<T> T getForObject(String url, Class<T> responseType, Map<String, ?> urlVariables)
Retrieve a representation by doing a GET on the URI template.
<T> T getForObject(URI url, Class<T> responseType)
Retrieve a representation by doing a GET on the URL .
<T> T getForObject(String url, Class<T> responseType, Object... urlVariables)
Retrieve a representation by doing a GET on the specified URL.
List<HttpMessageConverter<?>> getMessageConverters()
Returns the message body converters.
HttpHeaders headForHeaders(String url, Object... urlVariables)
Retrieve all headers of the resource specified by the URI template.
HttpHeaders headForHeaders(URI url)
Retrieve all headers of the resource specified by the URL.
HttpHeaders headForHeaders(String url, Map<String, ?> urlVariables)
Retrieve all headers of the resource specified by the URI template.
Set<HttpMethod> optionsForAllow(URI url)
Return the value of the Allow header for the given URL.
Set<HttpMethod> optionsForAllow(String url, Map<String, ?> urlVariables)
Return the value of the Allow header for the given URI.
Set<HttpMethod> optionsForAllow(String url, Object... urlVariables)
Return the value of the Allow header for the given URI.
<T> ResponseEntity<T> 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.
<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.
<T> ResponseEntity<T> 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.
URI postForLocation(String url, Object request, Map<String, ?> urlVariables)
Create a new resource by POSTing the given object to the URI template, and returns the value of the Location header.
URI postForLocation(URI url, Object request)
Create a new resource by POSTing the given object to the URL, and returns the value of the Location header.
URI postForLocation(String url, Object request, Object... urlVariables)
Create a new resource by POSTing the given object to the URI template, and returns the value of the Location header.
<T> T 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.
<T> T 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.
<T> T 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.
void put(URI url, Object request)
Creates a new resource by PUTting the given object to URL.
void put(String url, Object request, Object... urlVariables)
Create or update a resource by PUTting the given object to the URI.
void put(String url, Object request, Map<String, ?> urlVariables)
Creates a new resource by PUTting the given object to URI template.
void setErrorHandler(ResponseErrorHandler errorHandler)
Set the error handler.
void setMessageConverters(List<HttpMessageConverter<?>> messageConverters)
Set the message body converters to use.
Protected Methods
<T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor)
Execute the given method on the provided URI.
[Expand]
Inherited Methods
From class org.springframework.http.client.support.InterceptingHttpAccessor
From class org.springframework.http.client.support.HttpAccessor
From class java.lang.Object
From interface org.springframework.web.client.RestOperations

Public Constructors

public RestTemplate ()

Create a new instance of the RestTemplate using default settings.

public RestTemplate (ClientHttpRequestFactory requestFactory)

Create a new instance of the RestTemplate based on the given ClientHttpRequestFactory.

Parameters
requestFactory HTTP request factory to use

Public Methods

public void delete (String url, Object... urlVariables)

Delete the resources at the specified URI.

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

Parameters
url the URL
urlVariables the variables to expand in the template

public void delete (URI url)

Delete the resources at the specified URL.

Parameters
url the URL

public void delete (String url, Map<String, ?> urlVariables)

Delete the resources at the specified URI.

URI Template variables are expanded using the given map.

Parameters
url the URL
urlVariables the variables to expand the template

public ResponseEntity<T> 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.

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

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 of the return value
uriVariables the variables to expand in the template
Returns
  • the response as entity

public ResponseEntity<T> 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.

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 of the return value
Returns
  • the response as entity

public ResponseEntity<T> 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.

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

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 of the return value
uriVariables the variables to expand in the template
Returns
  • the response as entity

public T 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.

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

public T execute (String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Object... urlVariables)

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.

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
urlVariables the variables to expand in the template
Returns

public T execute (String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Map<String, ?> urlVariables)

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.

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
urlVariables the variables to expand in the template
Returns

public ResponseErrorHandler getErrorHandler ()

Return the error handler. By default, this is the DefaultResponseErrorHandler.

public ResponseEntity<T> getForEntity (URI url, Class<T> responseType)

Retrieve a representation by doing a GET on the URL . The response is converted and stored in an ResponseEntity.

Parameters
url the URL
responseType the type of the return value
Returns
  • the converted object

public ResponseEntity<T> getForEntity (String url, Class<T> responseType, Map<String, ?> urlVariables)

Retrieve a representation by doing a GET on the URI template. The response is converted and stored in an ResponseEntity.

URI Template variables are expanded using the given map.

Parameters
url the URL
responseType the type of the return value
urlVariables the map containing variables for the URI template
Returns
  • the converted object

public ResponseEntity<T> getForEntity (String url, Class<T> responseType, Object... urlVariables)

Retrieve an entity by doing a GET on the specified URL. The response is converted and stored in an ResponseEntity.

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

Parameters
url the URL
responseType the type of the return value
urlVariables the variables to expand the template
Returns
  • the entity

public T getForObject (String url, Class<T> responseType, Map<String, ?> urlVariables)

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.

Parameters
url the URL
responseType the type of the return value
urlVariables the map containing variables for the URI template
Returns
  • the converted object

public T getForObject (URI url, Class<T> responseType)

Retrieve a representation by doing a GET on the URL . The response (if any) is converted and returned.

Parameters
url the URL
responseType the type of the return value
Returns
  • the converted object

public T getForObject (String url, Class<T> responseType, Object... urlVariables)

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.

Parameters
url the URL
responseType the type of the return value
urlVariables the variables to expand the template
Returns
  • the converted object

public List<HttpMessageConverter<?>> getMessageConverters ()

Returns the message body converters. These converters are used to convert from and to HTTP requests and responses.

public HttpHeaders headForHeaders (String url, Object... urlVariables)

Retrieve all headers of the resource specified by the URI template.

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

Parameters
url the URL
urlVariables the variables to expand the template
Returns
  • all HTTP headers of that resource

public HttpHeaders headForHeaders (URI url)

Retrieve all headers of the resource specified by the URL.

Parameters
url the URL
Returns
  • all HTTP headers of that resource

public HttpHeaders headForHeaders (String url, Map<String, ?> urlVariables)

Retrieve all headers of the resource specified by the URI template.

URI Template variables are expanded using the given map.

Parameters
url the URL
urlVariables the map containing variables for the URI template
Returns
  • all HTTP headers of that resource

public Set<HttpMethod> optionsForAllow (URI url)

Return the value of the Allow header for the given URL.

Parameters
url the URL
Returns
  • the value of the allow header

public Set<HttpMethod> optionsForAllow (String url, Map<String, ?> urlVariables)

Return the value of the Allow header for the given URI.

URI Template variables are expanded using the given map.

Parameters
url the URL
urlVariables the variables to expand in the template
Returns
  • the value of the allow header

public Set<HttpMethod> optionsForAllow (String url, Object... urlVariables)

Return the value of the Allow header for the given URI.

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

Parameters
url the URL
urlVariables the variables to expand in the template
Returns
  • the value of the allow header

public ResponseEntity<T> 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.

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.

Parameters
url the URL
request the Object to be POSTed, may be null
uriVariables the variables to expand the template
Returns
  • the converted object

public 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.

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.

Parameters
url the URL
request the Object to be POSTed, may be null
uriVariables the variables to expand the template
Returns
  • the converted object

public ResponseEntity<T> 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.

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

Parameters
url the URL
request the Object to be POSTed, may be null
Returns
  • the converted object

public URI postForLocation (String url, Object request, Map<String, ?> urlVariables)

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.

Parameters
url the URL
request the Object to be POSTed, may be null
urlVariables the variables to expand the template
Returns
  • the value for the Location header

public URI postForLocation (URI url, Object request)

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.

Parameters
url the URL
request the Object to be POSTed, may be null
Returns
  • the value for the Location header

public URI postForLocation (String url, Object request, Object... urlVariables)

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.

Parameters
url the URL
request the Object to be POSTed, may be null
urlVariables the variables to expand the template
Returns
  • the value for the Location header

public T 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.

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.

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

public T 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.

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

Parameters
url the URL
request the Object to be POSTed, may be null
responseType the type of the return value
Returns
  • the converted object

public T 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.

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.

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

public void put (URI url, Object request)

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.

Parameters
url the URL
request the Object to be PUT, may be null

public void put (String url, Object request, Object... urlVariables)

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.

Parameters
url the URL
request the Object to be PUT, may be null
urlVariables the variables to expand the template

public void put (String url, Object request, Map<String, ?> urlVariables)

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.

Parameters
url the URL
request the Object to be PUT, may be null
urlVariables the variables to expand the template

public void setErrorHandler (ResponseErrorHandler errorHandler)

Set the error handler.

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.

Protected Methods

protected T doExecute (URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor)

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