org.springframework.web.client
Class RestTemplate

java.lang.Object
  extended by org.springframework.http.client.support.HttpAccessor
      extended by org.springframework.web.client.RestTemplate
All Implemented Interfaces:
RestOperations

public class RestTemplate
extends HttpAccessor
implements RestOperations

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(java.lang.String, java.lang.String...)
GETgetForObject(java.lang.String, java.lang.Class, java.lang.String...)
HEADheadForHeaders(java.lang.String, java.lang.String...)
OPTIONSoptionsForAllow(java.lang.String, java.lang.String...)
POSTpostForLocation(java.lang.String, java.lang.Object, java.lang.String...)
postForObject(java.lang.String, java.lang.Object, java.lang.Class, java.lang.String...)
PUTput(java.lang.String, java.lang.Object, java.lang.String...)
anyexecute(java.lang.String, org.springframework.http.HttpMethod, org.springframework.web.client.RequestCallback, org.springframework.web.client.ResponseExtractor, java.lang.String...)

Each of these methods takes uri template arguments in two forms: as a String variable arguments array, or as 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.

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.

Since:
3.0
Author:
Arjen Poutsma
See Also:
HttpMessageConverter, RequestCallback, ResponseExtractor, ResponseErrorHandler

Field Summary
 
Fields inherited from class org.springframework.http.client.support.HttpAccessor
logger
 
Constructor Summary
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.
 
Method Summary
 void delete(String url, Map<String,String> urlVariables)
          Delete the resources at the specified URI.
 void delete(String url, String... urlVariables)
          Delete the resources at the specified URI.
protected
<T> T
doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor)
          Execute the given method on the provided URI.
<T> T
execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Map<String,String> urlVariables)
          Execute the HTTP methods to the given URI, 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, String... urlVariables)
          Execute the HTTP methods to the given URI, preparing the request with the RequestCallback, and reading the response with a ResponseExtractor.
 ResponseErrorHandler getErrorHandler()
          Return the error handler.
<T> T
getForObject(String url, Class<T> responseType, Map<String,String> urlVariables)
          Retrieve a representation by doing a GET on the URI template.
<T> T
getForObject(String url, Class<T> responseType, String... urlVariables)
          Retrieve a representation by doing a GET on the specified URL.
 HttpMessageConverter<?>[] getMessageConverters()
          Returns the message body converters.
protected
<T> List<HttpMessageConverter<T>>
getSupportedMessageConverters(Class<T> type)
          Returns the message body converters that support a particular type.
 HttpHeaders headForHeaders(String url, Map<String,String> urlVariables)
          Retrieve all headers of the resource specified by the URI template.
 HttpHeaders headForHeaders(String url, String... urlVariables)
          Retrieve all headers of the resource specified by the URI template.
 Set<HttpMethod> optionsForAllow(String url, Map<String,String> urlVariables)
          Return the value of the Allow header for the given URI.
 Set<HttpMethod> optionsForAllow(String url, String... urlVariables)
          Return the value of the Allow header for the given URI.
 URI postForLocation(String url, Object request, Map<String,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(String url, Object request, String... 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,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(String url, Object request, Class<T> responseType, String... uriVariables)
          Create a new resource by POSTing the given object to the URI template, and returns the representation found in the response.
 void put(String url, Object request, Map<String,String> urlVariables)
          Creates a new resource by PUTting the given object to URI template.
 void put(String url, Object request, String... urlVariables)
          Create or update a resource by PUTting the given object to the URI.
 void setErrorHandler(ResponseErrorHandler errorHandler)
          Set the error handler.
 void setMessageConverters(HttpMessageConverter<?>[] messageConverters)
          Set the message body converters to use.
 
Methods inherited from class org.springframework.http.client.support.HttpAccessor
createRequest, getRequestFactory, setRequestFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RestTemplate

public RestTemplate()
Create a new instance of the RestTemplate using default settings.


RestTemplate

public RestTemplate(ClientHttpRequestFactory requestFactory)
Create a new instance of the RestTemplate based on the given ClientHttpRequestFactory.

Parameters:
requestFactory - HTTP request factory to use
See Also:
SimpleClientHttpRequestFactory, CommonsClientHttpRequestFactory
Method Detail

setMessageConverters

public void setMessageConverters(HttpMessageConverter<?>[] messageConverters)
Set the message body converters to use. These converters are used to convert from and to HTTP requests and responses.


getMessageConverters

public HttpMessageConverter<?>[] getMessageConverters()
Returns the message body converters. These converters are used to convert from and to HTTP requests and responses.


getSupportedMessageConverters

protected <T> List<HttpMessageConverter<T>> getSupportedMessageConverters(Class<T> type)
Returns the message body converters that support a particular type.

Parameters:
type - the type to return converters for
Returns:
converters that support the given type

setErrorHandler

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


getErrorHandler

public ResponseErrorHandler getErrorHandler()
Return the error handler. By default, this is the DefaultResponseErrorHandler.


getForObject

public <T> T getForObject(String url,
                          Class<T> responseType,
                          String... urlVariables)
               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
urlVariables - the variables to expand the template
Returns:
the converted object
Throws:
RestClientException

getForObject

public <T> T getForObject(String url,
                          Class<T> responseType,
                          Map<String,String> urlVariables)
               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
urlVariables - the map containing variables for the URI template
Returns:
the converted object
Throws:
RestClientException

headForHeaders

public HttpHeaders headForHeaders(String url,
                                  String... urlVariables)
                           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
urlVariables - the variables to expand the template
Returns:
all HTTP headers of that resource
Throws:
RestClientException

headForHeaders

public HttpHeaders headForHeaders(String url,
                                  Map<String,String> urlVariables)
                           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
urlVariables - the map containing variables for the URI template
Returns:
all HTTP headers of that resource
Throws:
RestClientException

postForLocation

public URI postForLocation(String url,
                           Object request,
                           String... urlVariables)
                    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.

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

postForLocation

public URI postForLocation(String url,
                           Object request,
                           Map<String,String> urlVariables)
                    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.

Specified by:
postForLocation in interface RestOperations
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
Throws:
RestClientException

postForObject

public <T> T postForObject(String url,
                           Object request,
                           Class<T> responseType,
                           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 URI variables, if any.

Specified by:
postForObject in interface RestOperations
Parameters:
url - the URL
request - the Object to be POSTed, may be null
Returns:
the converted object
Throws:
RestClientException

postForObject

public <T> T postForObject(String url,
                           Object request,
                           Class<T> responseType,
                           Map<String,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.

Specified by:
postForObject in interface RestOperations
Parameters:
url - the URL
request - the Object to be POSTed, may be null
Returns:
the converted object
Throws:
RestClientException

put

public void put(String url,
                Object request,
                String... urlVariables)
         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.

Specified by:
put in interface RestOperations
Parameters:
url - the URL
request - the Object to be PUT, may be null
urlVariables - the variables to expand the template
Throws:
RestClientException

put

public void put(String url,
                Object request,
                Map<String,String> urlVariables)
         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.

Specified by:
put in interface RestOperations
Parameters:
url - the URL
request - the Object to be PUT, may be null
urlVariables - the variables to expand the template
Throws:
RestClientException

delete

public void delete(String url,
                   String... urlVariables)
            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
urlVariables - the variables to expand in the template
Throws:
RestClientException

delete

public void delete(String url,
                   Map<String,String> urlVariables)
            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
urlVariables - the variables to expand the template
Throws:
RestClientException

optionsForAllow

public Set<HttpMethod> optionsForAllow(String url,
                                       String... urlVariables)
                                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
urlVariables - 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,String> urlVariables)
                                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
urlVariables - the variables to expand in the template
Returns:
the value of the allow header
Throws:
RestClientException

execute

public <T> T execute(String url,
                     HttpMethod method,
                     RequestCallback requestCallback,
                     ResponseExtractor<T> responseExtractor,
                     String... urlVariables)
          throws RestClientException
Description copied from interface: RestOperations
Execute the HTTP methods to the given URI, 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.

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
urlVariables - the variables to expand in the template
Returns:
an arbitrary object, as returned by the ResponseExtractor
Throws:
RestClientException

execute

public <T> T execute(String url,
                     HttpMethod method,
                     RequestCallback requestCallback,
                     ResponseExtractor<T> responseExtractor,
                     Map<String,String> urlVariables)
          throws RestClientException
Description copied from interface: RestOperations
Execute the HTTP methods to the given URI, preparing the request with the RequestCallback, and reading the response with a ResponseExtractor.

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

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
urlVariables - the variables to expand in the template
Returns:
an arbitrary object, as returned by the ResponseExtractor
Throws:
RestClientException

doExecute

protected <T> T doExecute(URI url,
                          HttpMethod method,
                          RequestCallback requestCallback,
                          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
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