extends
InterceptingHttpAccessor
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
DELETE
delete(java.lang.String, java.lang.Object...)
GET
getForObject(java.lang.String, java.lang.Class<T>, java.lang.Object...)
getForEntity(java.lang.String, java.lang.Class<T>, java.lang.Object...)
HEAD
headForHeaders(java.lang.String, java.lang.Object...)
OPTIONS
optionsForAllow(java.lang.String, java.lang.Object...)
POST
postForLocation(java.lang.String, java.lang.Object, java.lang.Object...)
postForObject(java.lang.String, java.lang.Object, java.lang.Class<T>, java.lang.Object...)
PUT
put(java.lang.String, java.lang.Object, java.lang.Object...)
any
exchange(java.lang.String, org.springframework.http.HttpMethod, org.springframework.http.HttpEntity<?>, java.lang.Class<T>, java.lang.Object...)
execute(java.lang.String, org.springframework.http.HttpMethod, org.springframework.web.client.RequestCallback, org.springframework.web.client.ResponseExtractor<T>, java.lang.Object...)
The
exchange
and
execute
methods are generalized versions of the more specific methods listed
above them. They support additional, less frequently used combinations including support for requests using the
HTTP PATCH method. However, note that the underlying HTTP library must also support the desired combination.
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.
-
Since:
-
Author:
-
Arjen Poutsma
-
See Also:
-
HttpMessageConverter
,
RequestCallback
,
ResponseExtractor
,
ResponseErrorHandler
HttpMethod
method,
RequestCallback
requestCallback,
ResponseExtractor
<T> responseExtractor)
Execute the given method on the provided URI.
<T>
ResponseEntity
<T>
exchange
(java.lang.String url,
HttpMethod
method,
HttpEntity
<?> requestEntity,
java.lang.Class<T> responseType,
java.util.Map<java.lang.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
(java.lang.String url,
HttpMethod
method,
HttpEntity
<?> requestEntity,
java.lang.Class<T> responseType,
java.lang.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>
ResponseEntity
<T>
exchange
(java.lang.String url,
HttpMethod
method,
HttpEntity
<?> requestEntity,
ParameterizedTypeReference
<T> responseType,
java.util.Map<java.lang.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
(java.lang.String url,
HttpMethod
method,
HttpEntity
<?> requestEntity,
ParameterizedTypeReference
<T> responseType,
java.lang.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>
ResponseEntity
<T>
exchange
(java.net.URI url,
HttpMethod
method,
HttpEntity
<?> requestEntity,
java.lang.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
(java.net.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
.
execute
(java.lang.String url,
HttpMethod
method,
RequestCallback
requestCallback,
ResponseExtractor
<T> responseExtractor,
java.util.Map<java.lang.String,?> urlVariables)
execute
(java.lang.String url,
HttpMethod
method,
RequestCallback
requestCallback,
ResponseExtractor
<T> responseExtractor,
java.lang.Object... urlVariables)
execute
(java.net.URI url,
HttpMethod
method,
RequestCallback
requestCallback,
ResponseExtractor
<T> responseExtractor)
ResponseErrorHandler
getErrorHandler
()
Return the error handler.
<T>
ResponseEntity
<T>
getForEntity
(java.lang.String url,
java.lang.Class<T> responseType,
java.util.Map<java.lang.String,?> urlVariables)
Retrieve a representation by doing a GET on the URI template.
<T>
ResponseEntity
<T>
getForEntity
(java.lang.String url,
java.lang.Class<T> responseType,
java.lang.Object... urlVariables)
Retrieve an entity by doing a GET on the specified URL.
<T>
ResponseEntity
<T>
getForEntity
(java.net.URI url,
java.lang.Class<T> responseType)
Retrieve a representation by doing a GET on the URL .
getForObject
(java.lang.String url,
java.lang.Class<T> responseType,
java.util.Map<java.lang.String,?> urlVariables)
Retrieve a representation by doing a GET on the URI template.
getForObject
(java.lang.String url,
java.lang.Class<T> responseType,
java.lang.Object... urlVariables)
Retrieve a representation by doing a GET on the specified URL.
getForObject
(java.net.URI url,
java.lang.Class<T> responseType)
Retrieve a representation by doing a GET on the URL .
java.util.List<
HttpMessageConverter
<?>>
getMessageConverters
()
Returns the message body converters.
private void
handleResponseError
(
HttpMethod
method,
java.net.URI url,
ClientHttpResponse
response)
HttpHeaders
headForHeaders
(java.lang.String url,
java.util.Map<java.lang.String,?> urlVariables)
Retrieve all headers of the resource specified by the URI template.
HttpHeaders
headForHeaders
(java.lang.String url,
java.lang.Object... urlVariables)
Retrieve all headers of the resource specified by the URI template.
HttpHeaders
headForHeaders
(java.net.URI url)
Retrieve all headers of the resource specified by the URL.
private void
logResponseStatus
(
HttpMethod
method,
java.net.URI url,
ClientHttpResponse
response)
java.util.Set<
HttpMethod
>
optionsForAllow
(java.lang.String url,
java.util.Map<java.lang.String,?> urlVariables)
Return the value of the Allow header for the given URI.
java.util.Set<
HttpMethod
>
optionsForAllow
(java.lang.String url,
java.lang.Object... urlVariables)
Return the value of the Allow header for the given URI.
java.util.Set<
HttpMethod
>
optionsForAllow
(java.net.URI url)
Return the value of the Allow header for the given URL.
<T>
ResponseEntity
<T>
postForEntity
(java.lang.String url,
java.lang.Object request,
java.lang.Class<T> responseType,
java.util.Map<java.lang.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
(java.lang.String url,
java.lang.Object request,
java.lang.Class<T> responseType,
java.lang.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
(java.net.URI url,
java.lang.Object request,
java.lang.Class<T> responseType)
Create a new resource by POSTing the given object to the URL,
and returns the response as
ResponseEntity
.
java.net.URI
postForLocation
(java.lang.String url,
java.lang.Object request,
java.util.Map<java.lang.String,?> urlVariables)
Create a new resource by POSTing the given object to the URI template, and returns the value of the
Location
header.
java.net.URI
postForLocation
(java.lang.String url,
java.lang.Object request,
java.lang.Object... urlVariables)
Create a new resource by POSTing the given object to the URI template, and returns the value of the
Location
header.
java.net.URI
postForLocation
(java.net.URI url,
java.lang.Object request)
Create a new resource by POSTing the given object to the URL, and returns the value of the
Location
header.
postForObject
(java.lang.String url,
java.lang.Object request,
java.lang.Class<T> responseType,
java.util.Map<java.lang.String,?> uriVariables)
Create a new resource by POSTing the given object to the URI template,
and returns the representation found in the response.
postForObject
(java.lang.String url,
java.lang.Object request,
java.lang.Class<T> responseType,
java.lang.Object... uriVariables)
Create a new resource by POSTing the given object to the URI template,
and returns the representation found in the response.
postForObject
(java.net.URI url,
java.lang.Object request,
java.lang.Class<T> responseType)
Create a new resource by POSTing the given object to the URL,
and returns the representation found in the response.
put
(java.lang.String url,
java.lang.Object request,
java.util.Map<java.lang.String,?> urlVariables)
Creates a new resource by PUTting the given object to URI template.
put
(java.lang.String url,
java.lang.Object request,
java.lang.Object... urlVariables)
Create or update a resource by PUTting the given object to the URI.
put
(java.net.URI url,
java.lang.Object request)
Creates a new resource by PUTting the given object to URL.
setErrorHandler
(
ResponseErrorHandler
errorHandler)
Set the error handler.
setMessageConverters
(java.util.List<
HttpMessageConverter
<?>> messageConverters)
Set the message body converters to use.
Methods inherited from class org.springframework.http.client.support.
InterceptingHttpAccessor
getInterceptors
,
getRequestFactory
,
setInterceptors
RestTemplate
public RestTemplate(ClientHttpRequestFactory requestFactory)
-
Parameters:
-
requestFactory
- HTTP request factory to use
-
See Also:
-
SimpleClientHttpRequestFactory
,
CommonsClientHttpRequestFactory
setMessageConverters
public void setMessageConverters(java.util.List<HttpMessageConverter<?>> messageConverters)
Set the message body converters to use. These converters are used to convert from and to HTTP requests and
responses.
getForObject
public <T> T getForObject(java.lang.String url,
java.lang.Class<T> responseType,
java.lang.Object... urlVariables)
throws RestClientException
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(java.lang.String url,
java.lang.Class<T> responseType,
java.util.Map<java.lang.String,?> urlVariables)
throws RestClientException
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
public <T> T getForObject(java.net.URI url,
java.lang.Class<T> responseType)
throws RestClientException
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(java.lang.String url,
java.lang.Class<T> responseType,
java.lang.Object... urlVariables)
throws RestClientException
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.
Specified by:
getForEntity
in interface
RestOperations
Parameters:
url
- the URL
responseType
- the type of the return value
urlVariables
- the variables to expand the template
Returns:
the entity
Throws:
RestClientException
getForEntity
public <T> ResponseEntity<T> getForEntity(java.lang.String url,
java.lang.Class<T> responseType,
java.util.Map<java.lang.String,?> urlVariables)
throws RestClientException
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.
Specified by:
getForEntity
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
getForEntity
public <T> ResponseEntity<T> getForEntity(java.net.URI url,
java.lang.Class<T> responseType)
throws RestClientException
Retrieve a representation by doing a GET on the URL .
The response is converted and stored in an
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(java.lang.String url,
java.lang.Object... urlVariables)
throws RestClientException
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(java.lang.String url,
java.util.Map<java.lang.String,?> urlVariables)
throws RestClientException
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
headForHeaders
public HttpHeaders headForHeaders(java.net.URI url)
throws RestClientException
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
postForLocation
public java.net.URI postForLocation(java.lang.String url,
java.lang.Object request,
java.lang.Object... urlVariables)
throws RestClientException
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.
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
See Also:
HttpEntity
postForLocation
public java.net.URI postForLocation(java.lang.String url,
java.lang.Object request,
java.util.Map<java.lang.String,?> urlVariables)
throws RestClientException
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.
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
See Also:
HttpEntity
postForLocation
public java.net.URI postForLocation(java.net.URI url,
java.lang.Object request)
throws RestClientException
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.
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
public <T> T postForObject(java.lang.String url,
java.lang.Object request,
java.lang.Class<T> responseType,
java.lang.Object... uriVariables)
throws RestClientException
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.
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
public <T> T postForObject(java.lang.String url,
java.lang.Object request,
java.lang.Class<T> responseType,
java.util.Map<java.lang.String,?> uriVariables)
throws RestClientException
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.
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
public <T> T postForObject(java.net.URI url,
java.lang.Object request,
java.lang.Class<T> responseType)
throws RestClientException
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.
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
postForEntity
public <T> ResponseEntity<T> postForEntity(java.lang.String url,
java.lang.Object request,
java.lang.Class<T> responseType,
java.lang.Object... uriVariables)
throws RestClientException
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.
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
postForEntity
public <T> ResponseEntity<T> postForEntity(java.lang.String url,
java.lang.Object request,
java.lang.Class<T> responseType,
java.util.Map<java.lang.String,?> uriVariables)
throws RestClientException
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.
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
postForEntity
public <T> ResponseEntity<T> postForEntity(java.net.URI url,
java.lang.Object request,
java.lang.Class<T> responseType)
throws RestClientException
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.
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
public void put(java.lang.String url,
java.lang.Object request,
java.lang.Object... urlVariables)
throws RestClientException
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
urlVariables
- the variables to expand the template
Throws:
RestClientException
See Also:
HttpEntity
public void put(java.lang.String url,
java.lang.Object request,
java.util.Map<java.lang.String,?> urlVariables)
throws RestClientException
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
urlVariables
- the variables to expand the template
Throws:
RestClientException
See Also:
HttpEntity
java.lang.Object request)
throws
RestClientException
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
public void delete(java.lang.String url,
java.lang.Object... urlVariables)
throws RestClientException
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(java.lang.String url,
java.util.Map<java.lang.String,?> urlVariables)
throws RestClientException
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
public void delete(java.net.URI url)
throws RestClientException
Delete the resources at the specified URL.
Specified by:
delete
in interface
RestOperations
Parameters:
url
- the URL
Throws:
RestClientException
optionsForAllow
public java.util.Set<HttpMethod> optionsForAllow(java.lang.String url,
java.lang.Object... urlVariables)
throws RestClientException
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 java.util.Set<HttpMethod> optionsForAllow(java.lang.String url,
java.util.Map<java.lang.String,?> urlVariables)
throws RestClientException
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
optionsForAllow
public java.util.Set<HttpMethod> optionsForAllow(java.net.URI url)
throws RestClientException
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
public <T> ResponseEntity<T> exchange(java.lang.String url,
HttpMethod method,
HttpEntity<?> requestEntity,
java.lang.Class<T> responseType,
java.lang.Object... uriVariables)
throws RestClientException
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 of the return value
uriVariables
- the variables to expand in the template
Returns:
the response as entity
Throws:
RestClientException
public <T> ResponseEntity<T> exchange(java.lang.String url,
HttpMethod method,
HttpEntity<?> requestEntity,
java.lang.Class<T> responseType,
java.util.Map<java.lang.String,?> uriVariables)
throws RestClientException
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 of the return value
uriVariables
- the variables to expand in the template
Returns:
the response as entity
Throws:
RestClientException
public <T> ResponseEntity<T> exchange(java.net.URI url,
HttpMethod method,
HttpEntity<?> requestEntity,
java.lang.Class<T> responseType)
throws RestClientException
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 of the return value
Returns:
the response as entity
Throws:
RestClientException
public <T> ResponseEntity<T> exchange(java.lang.String url,
HttpMethod method,
HttpEntity<?> requestEntity,
ParameterizedTypeReference<T> responseType,
java.lang.Object... uriVariables)
throws RestClientException
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("http://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 of the return value
uriVariables
- the variables to expand in the template
Returns:
the response as entity
Throws:
RestClientException
public <T> ResponseEntity<T> exchange(java.lang.String url,
HttpMethod method,
HttpEntity<?> requestEntity,
ParameterizedTypeReference<T> responseType,
java.util.Map<java.lang.String,?> uriVariables)
throws RestClientException
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("http://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 of the return value
uriVariables
- the variables to expand in the template
Returns:
the response as entity
Throws:
RestClientException
public <T> ResponseEntity<T> exchange(java.net.URI url,
HttpMethod method,
HttpEntity<?> requestEntity,
ParameterizedTypeReference<T> responseType)
throws RestClientException
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("http://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 of the return value
Returns:
the response as entity
Throws:
RestClientException
public <T> T execute(java.lang.String url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor,
java.lang.Object... urlVariables)
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.
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
public <T> T execute(java.lang.String url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor,
java.util.Map<java.lang.String,?> urlVariables)
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.
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
public <T> T execute(java.net.URI url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor)
throws RestClientException
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
protected <T> T doExecute(java.net.URI url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor)
throws RestClientException
-
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
handleResponseError
private void handleResponseError(HttpMethod method,
java.net.URI url,
ClientHttpResponse response)
throws java.io.IOException
-
Throws:
-
java.io.IOException