public class RestTemplate extends InterceptingHttpAccessor implements RestOperations
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
and execute
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("http://example.com/hotel list")
becomes
"http://example.com/hotel%20list"
. This also means if the URI template
or URI variables are already encoded, double encoding will occur, e.g.
http://example.com/hotel%20list
becomes
http://example.com/hotel%2520list
). To avoid that use a URI
method
variant to provide (or re-use) a previously encoded URI. To prepare such an URI
with full control over encoding, consider using
UriComponentsBuilder
.
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
via setMessageConverters(java.util.List<org.springframework.http.converter.HttpMessageConverter<?>>)
.
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 HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
and setErrorHandler(org.springframework.web.client.ResponseErrorHandler)
respectively.
HttpMessageConverter
,
RequestCallback
,
ResponseExtractor
,
ResponseErrorHandler
,
AsyncRestTemplate
logger
Constructor and Description |
---|
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 . |
RestTemplate(java.util.List<HttpMessageConverter<?>> messageConverters)
Create a new instance of the
RestTemplate using the given list of
HttpMessageConverter to use |
Modifier and Type | Method and Description |
---|---|
protected <T> RequestCallback |
acceptHeaderRequestCallback(java.lang.Class<T> responseType)
Returns a request callback implementation that prepares the request
Accept
headers based on the given response type and configured
message converters. |
void |
delete(java.lang.String url,
java.util.Map<java.lang.String,?> uriVariables)
Delete the resources at the specified URI.
|
void |
delete(java.lang.String url,
java.lang.Object... uriVariables)
Delete the resources at the specified URI.
|
void |
delete(java.net.URI url)
Delete the resources at the specified URL.
|
protected <T> T |
doExecute(java.net.URI url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor)
Execute the given method on the provided URI.
|
<T> ResponseEntity<T> |
exchange(RequestEntity<?> requestEntity,
java.lang.Class<T> responseType)
Execute the request specified in the given
RequestEntity and return
the response as ResponseEntity . |
<T> ResponseEntity<T> |
exchange(RequestEntity<?> requestEntity,
ParameterizedTypeReference<T> responseType)
Execute the request specified in the given
RequestEntity and return
the response as ResponseEntity . |
<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 . |
<T> T |
execute(java.lang.String url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor,
java.util.Map<java.lang.String,?> uriVariables)
Execute the HTTP method to the given URI template, preparing the request with the
RequestCallback , and reading the response with a ResponseExtractor . |
<T> T |
execute(java.lang.String url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor,
java.lang.Object... uriVariables)
Execute the HTTP method to the given URI template, preparing the request with the
RequestCallback , and reading the response with a ResponseExtractor . |
<T> T |
execute(java.net.URI url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor)
Execute the HTTP method to the given URL, preparing the request with the
RequestCallback , and reading the response with a ResponseExtractor . |
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,?> uriVariables)
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... uriVariables)
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 .
|
<T> T |
getForObject(java.lang.String url,
java.lang.Class<T> responseType,
java.util.Map<java.lang.String,?> uriVariables)
Retrieve a representation by doing a GET on the URI template.
|
<T> T |
getForObject(java.lang.String url,
java.lang.Class<T> responseType,
java.lang.Object... uriVariables)
Retrieve a representation by doing a GET on the specified URL.
|
<T> T |
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()
Return the message body converters.
|
UriTemplateHandler |
getUriTemplateHandler()
Return the configured URI template handler.
|
protected void |
handleResponse(java.net.URI url,
HttpMethod method,
ClientHttpResponse response)
Handle the given response, performing appropriate logging and
invoking the
ResponseErrorHandler if necessary. |
protected ResponseExtractor<HttpHeaders> |
headersExtractor()
Returns a response extractor for
HttpHeaders . |
HttpHeaders |
headForHeaders(java.lang.String url,
java.util.Map<java.lang.String,?> uriVariables)
Retrieve all headers of the resource specified by the URI template.
|
HttpHeaders |
headForHeaders(java.lang.String url,
java.lang.Object... uriVariables)
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.
|
protected <T> RequestCallback |
httpEntityCallback(java.lang.Object requestBody)
Returns a request callback implementation that writes the given object to the
request stream.
|
protected <T> RequestCallback |
httpEntityCallback(java.lang.Object requestBody,
java.lang.reflect.Type responseType)
Returns a request callback implementation that writes the given object to the
request stream.
|
java.util.Set<HttpMethod> |
optionsForAllow(java.lang.String url,
java.util.Map<java.lang.String,?> uriVariables)
Return the value of the Allow header for the given URI.
|
java.util.Set<HttpMethod> |
optionsForAllow(java.lang.String url,
java.lang.Object... uriVariables)
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> T |
patchForObject(java.lang.String url,
java.lang.Object request,
java.lang.Class<T> responseType,
java.util.Map<java.lang.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(java.lang.String url,
java.lang.Object request,
java.lang.Class<T> responseType,
java.lang.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(java.net.URI url,
java.lang.Object request,
java.lang.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(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,?> uriVariables)
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... uriVariables)
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. |
<T> T |
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.
|
<T> T |
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.
|
<T> T |
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.
|
void |
put(java.lang.String url,
java.lang.Object request,
java.util.Map<java.lang.String,?> uriVariables)
Creates a new resource by PUTting the given object to URI template.
|
void |
put(java.lang.String url,
java.lang.Object request,
java.lang.Object... uriVariables)
Create or update a resource by PUTting the given object to the URI.
|
void |
put(java.net.URI url,
java.lang.Object request)
Creates a new resource by PUTting the given object to URL.
|
protected <T> ResponseExtractor<ResponseEntity<T>> |
responseEntityExtractor(java.lang.reflect.Type responseType)
Returns a response extractor for
ResponseEntity . |
void |
setDefaultUriVariables(java.util.Map<java.lang.String,?> uriVars)
Configure default URI variable values.
|
void |
setErrorHandler(ResponseErrorHandler errorHandler)
Set the error handler.
|
void |
setMessageConverters(java.util.List<HttpMessageConverter<?>> messageConverters)
Set the message body converters to use.
|
void |
setUriTemplateHandler(UriTemplateHandler handler)
Configure the
UriTemplateHandler to use to expand URI templates. |
getInterceptors, getRequestFactory, setInterceptors
createRequest, setRequestFactory
public RestTemplate()
RestTemplate
using default settings.
Default HttpMessageConverter
s are initialized.public RestTemplate(ClientHttpRequestFactory requestFactory)
RestTemplate
based on the given ClientHttpRequestFactory
.requestFactory
- HTTP request factory to useSimpleClientHttpRequestFactory
,
HttpComponentsClientHttpRequestFactory
public RestTemplate(java.util.List<HttpMessageConverter<?>> messageConverters)
RestTemplate
using the given list of
HttpMessageConverter
to usemessageConverters
- the list of HttpMessageConverter
to usepublic void setMessageConverters(java.util.List<HttpMessageConverter<?>> messageConverters)
These converters are used to convert from and to HTTP requests and responses.
public java.util.List<HttpMessageConverter<?>> getMessageConverters()
public void setErrorHandler(ResponseErrorHandler errorHandler)
By default, RestTemplate uses a DefaultResponseErrorHandler
.
public ResponseErrorHandler getErrorHandler()
public void setDefaultUriVariables(java.util.Map<java.lang.String,?> uriVars)
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(); handler.setDefaultUriVariables(...); RestTemplate restTemplate = new RestTemplate(); restTemplate.setUriTemplateHandler(handler);
uriVars
- the default URI variable valuespublic void setUriTemplateHandler(UriTemplateHandler handler)
UriTemplateHandler
to use to expand URI templates.
By default the DefaultUriBuilderFactory
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.handler
- the URI template handler to usepublic UriTemplateHandler getUriTemplateHandler()
@Nullable public <T> T getForObject(java.lang.String url, java.lang.Class<T> responseType, java.lang.Object... uriVariables) throws RestClientException
RestOperations
URI Template variables are expanded using the given URI variables, if any.
getForObject
in interface RestOperations
url
- the URLresponseType
- the type of the return valueuriVariables
- the variables to expand the templateRestClientException
@Nullable public <T> T getForObject(java.lang.String url, java.lang.Class<T> responseType, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
URI Template variables are expanded using the given map.
getForObject
in interface RestOperations
url
- the URLresponseType
- the type of the return valueuriVariables
- the map containing variables for the URI templateRestClientException
@Nullable public <T> T getForObject(java.net.URI url, java.lang.Class<T> responseType) throws RestClientException
RestOperations
getForObject
in interface RestOperations
url
- the URLresponseType
- the type of the return valueRestClientException
public <T> ResponseEntity<T> getForEntity(java.lang.String url, java.lang.Class<T> responseType, java.lang.Object... uriVariables) throws RestClientException
RestOperations
ResponseEntity
.
URI Template variables are expanded using the given URI variables, if any.
getForEntity
in interface RestOperations
url
- the URLresponseType
- the type of the return valueuriVariables
- the variables to expand the templateRestClientException
public <T> ResponseEntity<T> getForEntity(java.lang.String url, java.lang.Class<T> responseType, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
ResponseEntity
.
URI Template variables are expanded using the given map.
getForEntity
in interface RestOperations
url
- the URLresponseType
- the type of the return valueuriVariables
- the map containing variables for the URI templateRestClientException
public <T> ResponseEntity<T> getForEntity(java.net.URI url, java.lang.Class<T> responseType) throws RestClientException
RestOperations
ResponseEntity
.getForEntity
in interface RestOperations
url
- the URLresponseType
- the type of the return valueRestClientException
public HttpHeaders headForHeaders(java.lang.String url, java.lang.Object... uriVariables) throws RestClientException
RestOperations
URI Template variables are expanded using the given URI variables, if any.
headForHeaders
in interface RestOperations
url
- the URLuriVariables
- the variables to expand the templateRestClientException
public HttpHeaders headForHeaders(java.lang.String url, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
URI Template variables are expanded using the given map.
headForHeaders
in interface RestOperations
url
- the URLuriVariables
- the map containing variables for the URI templateRestClientException
public HttpHeaders headForHeaders(java.net.URI url) throws RestClientException
RestOperations
headForHeaders
in interface RestOperations
url
- the URLRestClientException
@Nullable public java.net.URI postForLocation(java.lang.String url, @Nullable java.lang.Object request, java.lang.Object... uriVariables) throws RestClientException
RestOperations
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.
postForLocation
in interface RestOperations
url
- the URLrequest
- the Object to be POSTed (may be null
)uriVariables
- the variables to expand the templateLocation
headerRestClientException
HttpEntity
@Nullable public java.net.URI postForLocation(java.lang.String url, @Nullable java.lang.Object request, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
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.
postForLocation
in interface RestOperations
url
- the URLrequest
- the Object to be POSTed (may be null
)uriVariables
- the variables to expand the templateLocation
headerRestClientException
HttpEntity
@Nullable public java.net.URI postForLocation(java.net.URI url, @Nullable java.lang.Object request) throws RestClientException
RestOperations
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.
postForLocation
in interface RestOperations
url
- the URLrequest
- the Object to be POSTed (may be null
)Location
headerRestClientException
HttpEntity
@Nullable public <T> T postForObject(java.lang.String url, @Nullable java.lang.Object request, java.lang.Class<T> responseType, java.lang.Object... uriVariables) throws RestClientException
RestOperations
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.
postForObject
in interface RestOperations
url
- the URLrequest
- the Object to be POSTed (may be null
)responseType
- the type of the return valueuriVariables
- the variables to expand the templateRestClientException
HttpEntity
@Nullable public <T> T postForObject(java.lang.String url, @Nullable java.lang.Object request, java.lang.Class<T> responseType, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
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.
postForObject
in interface RestOperations
url
- the URLrequest
- the Object to be POSTed (may be null
)responseType
- the type of the return valueuriVariables
- the variables to expand the templateRestClientException
HttpEntity
@Nullable public <T> T postForObject(java.net.URI url, @Nullable java.lang.Object request, java.lang.Class<T> responseType) throws RestClientException
RestOperations
The request
parameter can be a HttpEntity
in order to
add additional HTTP headers to the request.
postForObject
in interface RestOperations
url
- the URLrequest
- the Object to be POSTed (may be null
)responseType
- the type of the return valueRestClientException
HttpEntity
public <T> ResponseEntity<T> postForEntity(java.lang.String url, @Nullable java.lang.Object request, java.lang.Class<T> responseType, java.lang.Object... uriVariables) throws RestClientException
RestOperations
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.
postForEntity
in interface RestOperations
url
- the URLrequest
- the Object to be POSTed (may be null
)uriVariables
- the variables to expand the templateRestClientException
HttpEntity
public <T> ResponseEntity<T> postForEntity(java.lang.String url, @Nullable java.lang.Object request, java.lang.Class<T> responseType, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
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.
postForEntity
in interface RestOperations
url
- the URLrequest
- the Object to be POSTed (may be null
)uriVariables
- the variables to expand the templateRestClientException
HttpEntity
public <T> ResponseEntity<T> postForEntity(java.net.URI url, @Nullable java.lang.Object request, java.lang.Class<T> responseType) throws RestClientException
RestOperations
ResponseEntity
.
The request
parameter can be a HttpEntity
in order to
add additional HTTP headers to the request.
postForEntity
in interface RestOperations
url
- the URLrequest
- the Object to be POSTed (may be null
)RestClientException
HttpEntity
public void put(java.lang.String url, @Nullable java.lang.Object request, java.lang.Object... uriVariables) throws RestClientException
RestOperations
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.
put
in interface RestOperations
url
- the URLrequest
- the Object to be PUT (may be null
)uriVariables
- the variables to expand the templateRestClientException
HttpEntity
public void put(java.lang.String url, @Nullable java.lang.Object request, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
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.
put
in interface RestOperations
url
- the URLrequest
- the Object to be PUT (may be null
)uriVariables
- the variables to expand the templateRestClientException
HttpEntity
public void put(java.net.URI url, @Nullable java.lang.Object request) throws RestClientException
RestOperations
The request
parameter can be a HttpEntity
in order to
add additional HTTP headers to the request.
put
in interface RestOperations
url
- the URLrequest
- the Object to be PUT (may be null
)RestClientException
HttpEntity
@Nullable public <T> T patchForObject(java.lang.String url, @Nullable java.lang.Object request, java.lang.Class<T> responseType, java.lang.Object... uriVariables) throws RestClientException
RestOperations
URI Template variables are expanded using the given URI variables, if any.
The request
parameter can be a HttpEntity
in order to
add additional HTTP headers to the request.
NOTE: The standard JDK HTTP library does not support HTTP PATCH. You need to use the Apache HttpComponents or OkHttp request factory.
patchForObject
in interface RestOperations
url
- the URLrequest
- the object to be PATCHed (may be null
)responseType
- the type of the return valueuriVariables
- the variables to expand the templateRestClientException
HttpEntity
,
HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
,
HttpComponentsAsyncClientHttpRequestFactory
,
OkHttp3ClientHttpRequestFactory
@Nullable public <T> T patchForObject(java.lang.String url, @Nullable java.lang.Object request, java.lang.Class<T> responseType, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
URI Template variables are expanded using the given map.
The request
parameter can be a HttpEntity
in order to
add additional HTTP headers to the request.
NOTE: The standard JDK HTTP library does not support HTTP PATCH. You need to use the Apache HttpComponents or OkHttp request factory.
patchForObject
in interface RestOperations
url
- the URLrequest
- the object to be PATCHed (may be null
)responseType
- the type of the return valueuriVariables
- the variables to expand the templateRestClientException
HttpEntity
,
HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
,
HttpComponentsAsyncClientHttpRequestFactory
,
OkHttp3ClientHttpRequestFactory
@Nullable public <T> T patchForObject(java.net.URI url, @Nullable java.lang.Object request, java.lang.Class<T> responseType) throws RestClientException
RestOperations
The request
parameter can be a HttpEntity
in order to
add additional HTTP headers to the request.
NOTE: The standard JDK HTTP library does not support HTTP PATCH. You need to use the Apache HttpComponents or OkHttp request factory.
patchForObject
in interface RestOperations
url
- the URLrequest
- the object to be PATCHed (may be null
)responseType
- the type of the return valueRestClientException
HttpEntity
,
HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
,
HttpComponentsAsyncClientHttpRequestFactory
,
OkHttp3ClientHttpRequestFactory
public void delete(java.lang.String url, java.lang.Object... uriVariables) throws RestClientException
RestOperations
URI Template variables are expanded using the given URI variables, if any.
delete
in interface RestOperations
url
- the URLuriVariables
- the variables to expand in the templateRestClientException
public void delete(java.lang.String url, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
URI Template variables are expanded using the given map.
delete
in interface RestOperations
url
- the URLuriVariables
- the variables to expand the templateRestClientException
public void delete(java.net.URI url) throws RestClientException
RestOperations
delete
in interface RestOperations
url
- the URLRestClientException
public java.util.Set<HttpMethod> optionsForAllow(java.lang.String url, java.lang.Object... uriVariables) throws RestClientException
RestOperations
URI Template variables are expanded using the given URI variables, if any.
optionsForAllow
in interface RestOperations
url
- the URLuriVariables
- the variables to expand in the templateRestClientException
public java.util.Set<HttpMethod> optionsForAllow(java.lang.String url, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
URI Template variables are expanded using the given map.
optionsForAllow
in interface RestOperations
url
- the URLuriVariables
- the variables to expand in the templateRestClientException
public java.util.Set<HttpMethod> optionsForAllow(java.net.URI url) throws RestClientException
RestOperations
optionsForAllow
in interface RestOperations
url
- the URLRestClientException
public <T> ResponseEntity<T> exchange(java.lang.String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, java.lang.Class<T> responseType, java.lang.Object... uriVariables) throws RestClientException
RestOperations
ResponseEntity
.
URI Template variables are expanded using the given URI variables, if any.
exchange
in interface RestOperations
url
- the URLmethod
- 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 valueuriVariables
- the variables to expand in the templateRestClientException
public <T> ResponseEntity<T> exchange(java.lang.String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, java.lang.Class<T> responseType, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
ResponseEntity
.
URI Template variables are expanded using the given URI variables, if any.
exchange
in interface RestOperations
url
- the URLmethod
- 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 valueuriVariables
- the variables to expand in the templateRestClientException
public <T> ResponseEntity<T> exchange(java.net.URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, java.lang.Class<T> responseType) throws RestClientException
RestOperations
ResponseEntity
.exchange
in interface RestOperations
url
- the URLmethod
- 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 valueRestClientException
public <T> ResponseEntity<T> exchange(java.lang.String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, java.lang.Object... uriVariables) throws RestClientException
RestOperations
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);
exchange
in interface RestOperations
url
- the URLmethod
- 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 valueuriVariables
- the variables to expand in the templateRestClientException
public <T> ResponseEntity<T> exchange(java.lang.String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
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);
exchange
in interface RestOperations
url
- the URLmethod
- 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 valueuriVariables
- the variables to expand in the templateRestClientException
public <T> ResponseEntity<T> exchange(java.net.URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException
RestOperations
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);
exchange
in interface RestOperations
url
- the URLmethod
- 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 valueRestClientException
public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, java.lang.Class<T> responseType) throws RestClientException
RestOperations
RequestEntity
and return
the response as ResponseEntity
. Typically used in combination
with the static builder methods on RequestEntity
, for instance:
MyRequest body = ... RequestEntity request = RequestEntity .post(new URI("http://example.com/foo")) .accept(MediaType.APPLICATION_JSON) .body(body); ResponseEntity<MyResponse> response = template.exchange(request, MyResponse.class);
exchange
in interface RestOperations
requestEntity
- the entity to write to the requestresponseType
- the type of the return valueRestClientException
public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException
RestOperations
RequestEntity
and return
the response as ResponseEntity
. The given
ParameterizedTypeReference
is used to pass generic type information:
MyRequest body = ... RequestEntity request = RequestEntity .post(new URI("http://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);
exchange
in interface RestOperations
requestEntity
- the entity to write to the requestresponseType
- the type of the return valueRestClientException
@Nullable public <T> T execute(java.lang.String url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, java.lang.Object... uriVariables) throws RestClientException
RestOperations
RequestCallback
, and reading the response with a ResponseExtractor
.
URI Template variables are expanded using the given URI variables, if any.
execute
in interface RestOperations
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 templateResponseExtractor
RestClientException
@Nullable public <T> T execute(java.lang.String url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, java.util.Map<java.lang.String,?> uriVariables) throws RestClientException
RestOperations
RequestCallback
, and reading the response with a ResponseExtractor
.
URI Template variables are expanded using the given URI variables map.
execute
in interface RestOperations
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 templateResponseExtractor
RestClientException
@Nullable public <T> T execute(java.net.URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) throws RestClientException
RestOperations
RequestCallback
, and reading the response with a ResponseExtractor
.execute
in interface RestOperations
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestCallback
- object that prepares the requestresponseExtractor
- object that extracts the return value from the responseResponseExtractor
RestClientException
@Nullable protected <T> T doExecute(java.net.URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) throws RestClientException
The ClientHttpRequest
is processed using the RequestCallback
;
the response with the ResponseExtractor
.
url
- the fully-expanded URL to connect tomethod
- 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
)ResponseExtractor
RestClientException
protected void handleResponse(java.net.URI url, HttpMethod method, ClientHttpResponse response) throws java.io.IOException
ResponseErrorHandler
if necessary.
Can be overridden in subclasses.
url
- the fully-expanded URL to connect tomethod
- the HTTP method to execute (GET, POST, etc.)response
- the resulting ClientHttpResponse
java.io.IOException
- if propagated from ResponseErrorHandler
setErrorHandler(org.springframework.web.client.ResponseErrorHandler)
protected <T> RequestCallback acceptHeaderRequestCallback(java.lang.Class<T> responseType)
Accept
headers based on the given response type and configured
message converters.protected <T> RequestCallback httpEntityCallback(@Nullable java.lang.Object requestBody)
protected <T> RequestCallback httpEntityCallback(@Nullable java.lang.Object requestBody, java.lang.reflect.Type responseType)
protected <T> ResponseExtractor<ResponseEntity<T>> responseEntityExtractor(java.lang.reflect.Type responseType)
ResponseEntity
.protected ResponseExtractor<HttpHeaders> headersExtractor()
HttpHeaders
.