Class RestTemplate
- java.lang.Object
- org.springframework.http.client.support.HttpAccessor
- org.springframework.http.client.support.InterceptingHttpAccessor
- org.springframework.web.client.RestTemplate
- All Implemented Interfaces:
RestOperations
public class RestTemplate extends InterceptingHttpAccessor implements RestOperations
Spring's central class for synchronous 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.Note: by default the RestTemplate relies on standard JDK facilities to establish HTTP connections. You can switch to use a different HTTP library such as Apache HttpComponents, Netty, and OkHttp through the
HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
property.The main entry points of this template are the methods named after the six main HTTP methods:
In addition the
exchange
andexecute
methods are generalized versions of the above methods and can be used to support additional, less frequent combinations (e.g. HTTP PATCH, HTTP PUT with response body, etc.). Note however that the underlying HTTP library used must also support the desired combination.For each HTTP method there are three variants: two accept a URI template string and URI variables (array or map) while a third accepts a
URI
. Note that for URI templates it is assumed encoding is necessary, e.g.restTemplate.getForObject("https://example.com/hotel list")
becomes"https://example.com/hotel%20list"
. This also means if the URI template or URI variables are already encoded, double encoding will occur, e.g.https://example.com/hotel%20list
becomeshttps://example.com/hotel%2520list
). To avoid that use aURI
method variant to provide (or re-use) a previously encoded URI. To prepare such an URI with full control over encoding, consider usingUriComponentsBuilder
.Internally the template uses
HttpMessageConverter
instances to convert HTTP messages to and from POJOs. Converters for the main mime types are registered by default but you can also register additional converters viasetMessageConverters(java.util.List<org.springframework.http.converter.HttpMessageConverter<?>>)
.This template uses a
SimpleClientHttpRequestFactory
and aDefaultResponseErrorHandler
as default strategies for creating HTTP connections or handling HTTP errors, respectively. These defaults can be overridden throughHttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
andsetErrorHandler(org.springframework.web.client.ResponseErrorHandler)
respectively.- Since:
- 3.0
- Author:
- Arjen Poutsma, Brian Clozel, Roy Clarkson, Juergen Hoeller
- See Also:
HttpMessageConverter
,RequestCallback
,ResponseExtractor
,ResponseErrorHandler
,AsyncRestTemplate
Field Summary
Fields inherited from class org.springframework.http.client.support.HttpAccessor
logger
Constructor Summary
Constructors Constructor Description RestTemplate()
Create a new instance of theRestTemplate
using default settings.RestTemplate(List<HttpMessageConverter<?>> messageConverters)
Create a new instance of theRestTemplate
using the given list ofHttpMessageConverter
to useRestTemplate(ClientHttpRequestFactory requestFactory)
Create a new instance of theRestTemplate
based on the givenClientHttpRequestFactory
.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T> RequestCallback
acceptHeaderRequestCallback(Class<T> responseType)
Returns a request callback implementation that prepares the requestAccept
headers based on the given response type and configured message converters.void
delete(String url, Object... uriVariables)
Delete the resources at the specified URI.void
delete(String url, Map<String,?> uriVariables)
Delete the resources at the specified URI.void
delete(URI url)
Delete the resources at the specified URL.protected <T> T
doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor)
Execute the given method on the provided URI.<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 asResponseEntity
.<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 asResponseEntity
.<T> ResponseEntity<T>
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 asResponseEntity
.<T> ResponseEntity<T>
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 asResponseEntity
.<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 asResponseEntity
.<T> ResponseEntity<T>
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 asResponseEntity
.<T> ResponseEntity<T>
exchange(RequestEntity<?> requestEntity, Class<T> responseType)
Execute the request specified in the givenRequestEntity
and return the response asResponseEntity
.<T> ResponseEntity<T>
exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType)
Execute the request specified in the givenRequestEntity
and return the response asResponseEntity
.<T> T
execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Object... uriVariables)
Execute the HTTP method to the given URI template, preparing the request with theRequestCallback
, and reading the response with aResponseExtractor
.<T> T
execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Map<String,?> uriVariables)
Execute the HTTP method to the given URI template, preparing the request with theRequestCallback
, and reading the response with aResponseExtractor
.<T> T
execute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor)
Execute the HTTP method to the given URL, preparing the request with theRequestCallback
, and reading the response with aResponseExtractor
.ResponseErrorHandler
getErrorHandler()
Return the error handler.<T> ResponseEntity<T>
getForEntity(String url, Class<T> responseType, Object... uriVariables)
Retrieve an entity by doing a GET on the specified URL.<T> ResponseEntity<T>
getForEntity(String url, Class<T> responseType, Map<String,?> uriVariables)
Retrieve a representation by doing a GET on the URI template.<T> ResponseEntity<T>
getForEntity(URI url, Class<T> responseType)
Retrieve a representation by doing a GET on the URL .<T> T
getForObject(String url, Class<T> responseType, Object... uriVariables)
Retrieve a representation by doing a GET on the specified URL.<T> T
getForObject(String url, Class<T> responseType, Map<String,?> uriVariables)
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 .List<HttpMessageConverter<?>>
getMessageConverters()
Return the list of message body converters.UriTemplateHandler
getUriTemplateHandler()
Return the configured URI template handler.protected void
handleResponse(URI url, HttpMethod method, ClientHttpResponse response)
Handle the given response, performing appropriate logging and invoking theResponseErrorHandler
if necessary.protected ResponseExtractor<HttpHeaders>
headersExtractor()
Returns a response extractor forHttpHeaders
.HttpHeaders
headForHeaders(String url, Object... uriVariables)
Retrieve all headers of the resource specified by the URI template.HttpHeaders
headForHeaders(String url, Map<String,?> uriVariables)
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.protected <T> RequestCallback
httpEntityCallback(Object requestBody)
Returns a request callback implementation that writes the given object to the request stream.protected <T> RequestCallback
httpEntityCallback(Object requestBody, Type responseType)
Returns a request callback implementation that writes the given object to the request stream.Set<HttpMethod>
optionsForAllow(String url, Object... uriVariables)
Return the value of the Allow header for the given URI.Set<HttpMethod>
optionsForAllow(String url, Map<String,?> uriVariables)
Return the value of the Allow header for the given URI.Set<HttpMethod>
optionsForAllow(URI url)
Return the value of the Allow header for the given URL.<T> T
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.<T> T
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.<T> T
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 asResponseEntity
.<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 asHttpEntity
.<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 asResponseEntity
.URI
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 theLocation
header.URI
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 theLocation
header.URI
postForLocation(URI url, Object request)
Create a new resource by POSTing the given object to the URL, and returns the value of theLocation
header.<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.<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.void
put(String url, Object request, Object... uriVariables)
Create or update a resource by PUTting the given object to the URI.void
put(String url, Object request, Map<String,?> uriVariables)
Creates a new resource by PUTting the given object to URI template.void
put(URI url, Object request)
Creates a new resource by PUTting the given object to URL.protected <T> ResponseExtractor<ResponseEntity<T>>
responseEntityExtractor(Type responseType)
Returns a response extractor forResponseEntity
.void
setDefaultUriVariables(Map<String,?> defaultUriVariables)
Configure default URI variable values.void
setErrorHandler(ResponseErrorHandler errorHandler)
Set the error handler.void
setMessageConverters(List<HttpMessageConverter<?>> messageConverters)
Set the message body converters to use.void
setUriTemplateHandler(UriTemplateHandler handler)
Configure theUriTemplateHandler
to use to expand URI templates.Methods inherited from class org.springframework.http.client.support.InterceptingHttpAccessor
getInterceptors, getRequestFactory, setInterceptors
Methods inherited from class org.springframework.http.client.support.HttpAccessor
createRequest, setRequestFactory
Constructor Detail
RestTemplate
public RestTemplate()
Create a new instance of theRestTemplate
using default settings. DefaultHttpMessageConverter
s are initialized.
RestTemplate
public RestTemplate(ClientHttpRequestFactory requestFactory)
Create a new instance of theRestTemplate
based on the givenClientHttpRequestFactory
.- Parameters:
requestFactory
- HTTP request factory to use- See Also:
SimpleClientHttpRequestFactory
,HttpComponentsClientHttpRequestFactory
RestTemplate
public RestTemplate(List<HttpMessageConverter<?>> messageConverters)
Create a new instance of theRestTemplate
using the given list ofHttpMessageConverter
to use- Parameters:
messageConverters
- the list ofHttpMessageConverter
to use- Since:
- 3.2.7
Method Detail
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
.
getErrorHandler
public ResponseErrorHandler getErrorHandler()
Return the error handler.
setDefaultUriVariables
public void setDefaultUriVariables(Map<String,?> defaultUriVariables)
Configure default URI variable values. This is a shortcut for:DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler(); handler.setDefaultUriVariables(...); RestTemplate restTemplate = new RestTemplate(); restTemplate.setUriTemplateHandler(handler);
- Parameters:
defaultUriVariables
- the default URI variable values- Since:
- 4.3
setUriTemplateHandler
public void setUriTemplateHandler(UriTemplateHandler handler)
Configure theUriTemplateHandler
to use to expand URI templates. By default theDefaultUriTemplateHandler
is used which relies on Spring's URI template support and exposes several useful properties that customize its behavior for encoding and for prepending a common base URL. An alternative implementation may be used to plug an external URI template library.- Parameters:
handler
- the URI template handler to use
getUriTemplateHandler
public UriTemplateHandler getUriTemplateHandler()
Return the configured URI template handler.
getForObject
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 interfaceRestOperations
- Parameters:
url
- the URLresponseType
- the type of the return valueuriVariables
- the variables to expand the template- Returns:
- the converted object
- Throws:
RestClientException
getForObject
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 interfaceRestOperations
- Parameters:
url
- the URLresponseType
- the type of the return valueuriVariables
- the map containing variables for the URI template- Returns:
- the converted object
- Throws:
RestClientException
getForObject
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 interfaceRestOperations
- Parameters:
url
- the URLresponseType
- 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 anResponseEntity
.URI Template variables are expanded using the given URI variables, if any.
- Specified by:
getForEntity
in interfaceRestOperations
- Parameters:
url
- the URLresponseType
- the type of the return valueuriVariables
- 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 anResponseEntity
.URI Template variables are expanded using the given map.
- Specified by:
getForEntity
in interfaceRestOperations
- Parameters:
url
- the URLresponseType
- the type of the return valueuriVariables
- 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 anResponseEntity
.- Specified by:
getForEntity
in interfaceRestOperations
- Parameters:
url
- the URLresponseType
- 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 interfaceRestOperations
- Parameters:
url
- the URLuriVariables
- 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 interfaceRestOperations
- Parameters:
url
- the URLuriVariables
- 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 interfaceRestOperations
- Parameters:
url
- the URL- Returns:
- all HTTP headers of that resource
- Throws:
RestClientException
postForLocation
public URI postForLocation(String url, Object request, 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 theLocation
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 aHttpEntity
in order to add additional HTTP headers to the request.- Specified by:
postForLocation
in interfaceRestOperations
- Parameters:
url
- the URLrequest
- the Object to be POSTed (may benull
)uriVariables
- the variables to expand the template- Returns:
- the value for the
Location
header - Throws:
RestClientException
- See Also:
HttpEntity
postForLocation
public URI postForLocation(String url, Object request, 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 theLocation
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 aHttpEntity
in order to add additional HTTP headers to the request.- Specified by:
postForLocation
in interfaceRestOperations
- Parameters:
url
- the URLrequest
- the Object to be POSTed (may benull
)uriVariables
- the variables to expand the template- Returns:
- the value for the
Location
header - Throws:
RestClientException
- See Also:
HttpEntity
postForLocation
public URI postForLocation(URI url, 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 theLocation
header. This header typically indicates where the new resource is stored.The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.- Specified by:
postForLocation
in interfaceRestOperations
- Parameters:
url
- the URLrequest
- the Object to be POSTed (may benull
)- Returns:
- the value for the
Location
header - Throws:
RestClientException
- See Also:
HttpEntity
postForObject
public <T> T postForObject(String url, Object request, Class<T> responseType, 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 aHttpEntity
in order to add additional HTTP headers to the request.- Specified by:
postForObject
in interfaceRestOperations
- Parameters:
url
- the URLrequest
- the Object to be POSTed (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand the template- Returns:
- the converted object
- Throws:
RestClientException
- See Also:
HttpEntity
postForObject
public <T> T postForObject(String url, Object request, Class<T> responseType, 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 aHttpEntity
in order to add additional HTTP headers to the request.- Specified by:
postForObject
in interfaceRestOperations
- Parameters:
url
- the URLrequest
- the Object to be POSTed (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand the template- Returns:
- the converted object
- Throws:
RestClientException
- See Also:
HttpEntity
postForObject
public <T> T postForObject(URI url, Object request, 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 aHttpEntity
in order to add additional HTTP headers to the request.- Specified by:
postForObject
in interfaceRestOperations
- Parameters:
url
- the URLrequest
- the Object to be POSTed (may benull
)responseType
- the type of the return value- Returns:
- the converted object
- Throws:
RestClientException
- See Also:
HttpEntity
postForEntity
public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, 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 asResponseEntity
.URI Template variables are expanded using the given URI variables, if any.
The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.- Specified by:
postForEntity
in interfaceRestOperations
- Parameters:
url
- the URLrequest
- the Object to be POSTed (may benull
)uriVariables
- the variables to expand the template- Returns:
- the converted object
- Throws:
RestClientException
- See Also:
HttpEntity
postForEntity
public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, 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 asHttpEntity
.URI Template variables are expanded using the given map.
The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.- Specified by:
postForEntity
in interfaceRestOperations
- Parameters:
url
- the URLrequest
- the Object to be POSTed (may benull
)uriVariables
- the variables to expand the template- Returns:
- the converted object
- Throws:
RestClientException
- See Also:
HttpEntity
postForEntity
public <T> ResponseEntity<T> postForEntity(URI url, Object request, 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 asResponseEntity
.The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.- Specified by:
postForEntity
in interfaceRestOperations
- Parameters:
url
- the URLrequest
- the Object to be POSTed (may benull
)- Returns:
- the converted object
- Throws:
RestClientException
- See Also:
HttpEntity
put
public void put(String url, Object request, 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 aHttpEntity
in order to add additional HTTP headers to the request.- Specified by:
put
in interfaceRestOperations
- Parameters:
url
- the URLrequest
- the Object to be PUT (may benull
)uriVariables
- the variables to expand the template- Throws:
RestClientException
- See Also:
HttpEntity
put
public void put(String url, Object request, 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 aHttpEntity
in order to add additional HTTP headers to the request.- Specified by:
put
in interfaceRestOperations
- Parameters:
url
- the URLrequest
- the Object to be PUT (may benull
)uriVariables
- the variables to expand the template- Throws:
RestClientException
- See Also:
HttpEntity
put
public void put(URI url, 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 aHttpEntity
in order to add additional HTTP headers to the request.- Specified by:
put
in interfaceRestOperations
- Parameters:
url
- the URLrequest
- the Object to be PUT (may benull
)- Throws:
RestClientException
- See Also:
HttpEntity
patchForObject
public <T> T patchForObject(String url, Object request, Class<T> responseType, 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 aHttpEntity
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 interfaceRestOperations
- Parameters:
url
- the URLrequest
- the object to be PATCHed (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand the template- Returns:
- the converted object
- Throws:
RestClientException
- See Also:
HttpEntity
,HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
,HttpComponentsAsyncClientHttpRequestFactory
,OkHttp3ClientHttpRequestFactory
patchForObject
public <T> T patchForObject(String url, Object request, Class<T> responseType, 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 aHttpEntity
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 interfaceRestOperations
- Parameters:
url
- the URLrequest
- the object to be PATCHed (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand the template- Returns:
- the converted object
- Throws:
RestClientException
- See Also:
HttpEntity
,HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
,HttpComponentsAsyncClientHttpRequestFactory
,OkHttp3ClientHttpRequestFactory
patchForObject
public <T> T patchForObject(URI url, Object request, 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 aHttpEntity
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 interfaceRestOperations
- Parameters:
url
- the URLrequest
- the object to be PATCHed (may benull
)responseType
- the type of the return value- Returns:
- the converted object
- Throws:
RestClientException
- See Also:
HttpEntity
,HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
,HttpComponentsAsyncClientHttpRequestFactory
,OkHttp3ClientHttpRequestFactory
delete
public void delete(String url, 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 interfaceRestOperations
- Parameters:
url
- the URLuriVariables
- the variables to expand in the template- Throws:
RestClientException
delete
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 interfaceRestOperations
- Parameters:
url
- the URLuriVariables
- the variables to expand the template- Throws:
RestClientException
delete
public void delete(URI url) throws RestClientException
Description copied from interface:RestOperations
Delete the resources at the specified URL.- Specified by:
delete
in interfaceRestOperations
- 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 interfaceRestOperations
- Parameters:
url
- the URLuriVariables
- 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 interfaceRestOperations
- Parameters:
url
- the URLuriVariables
- 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 interfaceRestOperations
- Parameters:
url
- the URL- Returns:
- the value of the allow header
- Throws:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<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 asResponseEntity
.URI Template variables are expanded using the given URI variables, if any.
- Specified by:
exchange
in interfaceRestOperations
- Parameters:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestEntity
- the entity (headers and/or body) to write to the request may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand in the template- Returns:
- the response as entity
- Throws:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<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 asResponseEntity
.URI Template variables are expanded using the given URI variables, if any.
- Specified by:
exchange
in interfaceRestOperations
- Parameters:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestEntity
- the entity (headers and/or body) to write to the request (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand in the template- Returns:
- the response as entity
- Throws:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, 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 asResponseEntity
.- Specified by:
exchange
in interfaceRestOperations
- Parameters:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestEntity
- the entity (headers and/or body) to write to the request (may benull
)responseType
- the type of the return value- Returns:
- the response as entity
- Throws:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, 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 asResponseEntity
. The givenParameterizedTypeReference
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 interfaceRestOperations
- Parameters:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestEntity
- the entity (headers and/or body) to write to the request (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand in the template- Returns:
- the response as entity
- Throws:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, 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 asResponseEntity
. The givenParameterizedTypeReference
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 interfaceRestOperations
- Parameters:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestEntity
- the entity (headers and/or body) to write to the request (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand in the template- Returns:
- the response as entity
- Throws:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, 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 asResponseEntity
. The givenParameterizedTypeReference
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 interfaceRestOperations
- Parameters:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestEntity
- the entity (headers and/or body) to write to the request (may benull
)responseType
- the type of the return value- Returns:
- the response as entity
- Throws:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, Class<T> responseType) throws RestClientException
Description copied from interface:RestOperations
Execute the request specified in the givenRequestEntity
and return the response asResponseEntity
. Typically used in combination with the static builder methods onRequestEntity
, for instance:MyRequest body = ... RequestEntity request = RequestEntity.post(new URI("https://example.com/foo")).accept(MediaType.APPLICATION_JSON).body(body); ResponseEntity<MyResponse> response = template.exchange(request, MyResponse.class);
- Specified by:
exchange
in interfaceRestOperations
- Parameters:
requestEntity
- the entity to write to the requestresponseType
- the type of the return value- Returns:
- the response as entity
- Throws:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException
Description copied from interface:RestOperations
Execute the request specified in the givenRequestEntity
and return the response asResponseEntity
. The givenParameterizedTypeReference
is used to pass generic type information:MyRequest body = ... RequestEntity request = RequestEntity.post(new URI("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 interfaceRestOperations
- Parameters:
requestEntity
- the entity to write to the requestresponseType
- the type of the return value- Returns:
- the response as entity
- Throws:
RestClientException
execute
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Object... uriVariables) throws RestClientException
Description copied from interface:RestOperations
Execute the HTTP method to the given URI template, preparing the request with theRequestCallback
, and reading the response with aResponseExtractor
.URI Template variables are expanded using the given URI variables, if any.
- Specified by:
execute
in interfaceRestOperations
- Parameters:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestCallback
- object that prepares the requestresponseExtractor
- object that extracts the return value from the responseuriVariables
- 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,?> uriVariables) throws RestClientException
Description copied from interface:RestOperations
Execute the HTTP method to the given URI template, preparing the request with theRequestCallback
, and reading the response with aResponseExtractor
.URI Template variables are expanded using the given URI variables map.
- Specified by:
execute
in interfaceRestOperations
- Parameters:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestCallback
- object that prepares the requestresponseExtractor
- object that extracts the return value from the responseuriVariables
- the variables to expand in the template- Returns:
- an arbitrary object, as returned by the
ResponseExtractor
- Throws:
RestClientException
execute
public <T> T execute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor) throws RestClientException
Description copied from interface:RestOperations
Execute the HTTP method to the given URL, preparing the request with theRequestCallback
, and reading the response with aResponseExtractor
.- Specified by:
execute
in interfaceRestOperations
- Parameters:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestCallback
- object that prepares the requestresponseExtractor
- object that extracts the return value from the response- 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 theRequestCallback
; the response with theResponseExtractor
.- Parameters:
url
- the fully-expanded URL to connect tomethod
- the HTTP method to execute (GET, POST, etc.)requestCallback
- object that prepares the request (can benull
)responseExtractor
- object that extracts the return value from the response (can benull
)- Returns:
- an arbitrary object, as returned by the
ResponseExtractor
- Throws:
RestClientException
handleResponse
protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException
Handle the given response, performing appropriate logging and invoking theResponseErrorHandler
if necessary.Can be overridden in subclasses.
- Parameters:
url
- the fully-expanded URL to connect tomethod
- the HTTP method to execute (GET, POST, etc.)response
- the resultingClientHttpResponse
- Throws:
IOException
- if propagated fromResponseErrorHandler
- Since:
- 4.1.6
- See Also:
setErrorHandler(org.springframework.web.client.ResponseErrorHandler)
acceptHeaderRequestCallback
protected <T> RequestCallback acceptHeaderRequestCallback(Class<T> responseType)
Returns a request callback implementation that prepares the requestAccept
headers based on the given response type and configured message converters.
httpEntityCallback
protected <T> RequestCallback httpEntityCallback(Object requestBody)
Returns a request callback implementation that writes the given object to the request stream.
httpEntityCallback
protected <T> RequestCallback httpEntityCallback(Object requestBody, Type responseType)
Returns a request callback implementation that writes the given object to the request stream.
responseEntityExtractor
protected <T> ResponseExtractor<ResponseEntity<T>> responseEntityExtractor(Type responseType)
Returns a response extractor forResponseEntity
.
headersExtractor
protected ResponseExtractor<HttpHeaders> headersExtractor()
Returns a response extractor forHttpHeaders
.