public class RestTemplate extends InterceptingHttpAccessor implements RestOperations
HttpURLConnection, Apache HttpComponents, and others.
The RestTemplate offers templates for common scenarios by HTTP method, in
addition to the generalized exchange and execute methods that
support of less frequent cases.
NOTE: As of 5.0 this class is in maintenance mode, with
only minor requests for changes and bugs to be accepted going forward. Please,
consider using the org.springframework.web.reactive.client.WebClient
which has a more modern API and supports sync, async, and streaming scenarios.
HttpMessageConverter,
RequestCallback,
ResponseExtractor,
ResponseErrorHandlerlogger| 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(List<HttpMessageConverter<?>> messageConverters)
Create a new instance of the
RestTemplate using the given list of
HttpMessageConverter to use. |
| Modifier and Type | Method and Description |
|---|---|
<T> RequestCallback |
acceptHeaderRequestCallback(Class<T> responseType)
Return a
RequestCallback that sets the request Accept
header based on the given response type, cross-checked against the
configured message converters. |
void |
delete(String url,
Map<String,?> uriVariables)
Delete the resources at the specified URI.
|
void |
delete(String url,
Object... 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(RequestEntity<?> entity,
Class<T> responseType)
Execute the request specified in the given
RequestEntity and return
the response as ResponseEntity. |
<T> ResponseEntity<T> |
exchange(RequestEntity<?> entity,
ParameterizedTypeReference<T> responseType)
Execute the request specified in the given
RequestEntity and return
the response as ResponseEntity. |
<T> ResponseEntity<T> |
exchange(String url,
HttpMethod method,
HttpEntity<?> requestEntity,
Class<T> responseType,
Map<String,?> uriVariables)
Execute the HTTP method to the given URI template, writing the given request entity to the request, and
returns the response as
ResponseEntity. |
<T> ResponseEntity<T> |
exchange(String url,
HttpMethod method,
HttpEntity<?> requestEntity,
Class<T> responseType,
Object... uriVariables)
Execute the HTTP method to the given URI template, writing the given request entity to the request, and
returns the response as
ResponseEntity. |
<T> 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 as
ResponseEntity. |
<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 as
ResponseEntity. |
<T> ResponseEntity<T> |
exchange(URI url,
HttpMethod method,
HttpEntity<?> requestEntity,
Class<T> responseType)
Execute the HTTP method to the given URI template, writing the given request entity to the request, and
returns the response as
ResponseEntity. |
<T> ResponseEntity<T> |
exchange(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(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 the
RequestCallback, and reading the response with a ResponseExtractor. |
<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 the
RequestCallback, and reading the response with a ResponseExtractor. |
<T> T |
execute(URI url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor)
Execute the HTTP method to the given URL, preparing the request with the
RequestCallback, and reading the response with a ResponseExtractor. |
ResponseErrorHandler |
getErrorHandler()
Return the error handler.
|
<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(String url,
Class<T> responseType,
Object... uriVariables)
Retrieve an entity by doing a GET on the specified URL.
|
<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,
Map<String,?> uriVariables)
Retrieve a representation by doing a GET on the URI template.
|
<T> T |
getForObject(String url,
Class<T> responseType,
Object... uriVariables)
Retrieve a representation by doing a GET on the specified URL.
|
<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 the
ResponseErrorHandler if necessary. |
protected ResponseExtractor<HttpHeaders> |
headersExtractor()
Return a response extractor for
HttpHeaders. |
HttpHeaders |
headForHeaders(String url,
Map<String,?> uriVariables)
Retrieve all headers of the resource specified by the URI template.
|
HttpHeaders |
headForHeaders(String url,
Object... 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.
|
<T> RequestCallback |
httpEntityCallback(Object requestBody)
Return a
RequestCallback implementation that writes the given
object to the request stream. |
<T> RequestCallback |
httpEntityCallback(Object requestBody,
Type responseType)
Return a
RequestCallback implementation that:
Sets the request Accept header based on the given response
type, cross-checked against the configured message converters. |
Set<HttpMethod> |
optionsForAllow(String url,
Map<String,?> uriVariables)
Return the value of the Allow header for the given URI.
|
Set<HttpMethod> |
optionsForAllow(String url,
Object... 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,
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(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(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,
Map<String,?> uriVariables)
Create a new resource by POSTing the given object to the URI template,
and returns the response as
HttpEntity. |
<T> ResponseEntity<T> |
postForEntity(String url,
Object request,
Class<T> responseType,
Object... uriVariables)
Create a new resource by POSTing the given object to the URI template,
and returns the response as
ResponseEntity. |
<T> ResponseEntity<T> |
postForEntity(URI url,
Object request,
Class<T> responseType)
Create a new resource by POSTing the given object to the URL,
and returns the response as
ResponseEntity. |
URI |
postForLocation(String url,
Object request,
Map<String,?> uriVariables)
Create a new resource by POSTing the given object to the URI template, and returns the value of
the
Location header. |
URI |
postForLocation(String url,
Object request,
Object... uriVariables)
Create a new resource by POSTing the given object to the URI template, and returns the value of
the
Location header. |
URI |
postForLocation(URI url,
Object request)
Create a new resource by POSTing the given object to the URL, and returns the value of the
Location header. |
<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(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(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,
Map<String,?> uriVariables)
Creates a new resource by PUTting the given object to URI template.
|
void |
put(String url,
Object request,
Object... uriVariables)
Create or update a resource by PUTting the given object to the URI.
|
void |
put(URI url,
Object request)
Creates a new resource by PUTting the given object to URL.
|
<T> ResponseExtractor<ResponseEntity<T>> |
responseEntityExtractor(Type responseType)
Return a
ResponseExtractor that prepares a ResponseEntity. |
void |
setDefaultUriVariables(Map<String,?> uriVars)
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 a strategy for expanding URI templates.
|
getInterceptors, getRequestFactory, setInterceptors, setRequestFactorycreateRequest, getClientHttpRequestInitializers, setClientHttpRequestInitializerspublic RestTemplate()
RestTemplate using default settings.
Default HttpMessageConverters are initialized.public RestTemplate(ClientHttpRequestFactory requestFactory)
RestTemplate based on the given ClientHttpRequestFactory.requestFactory - the HTTP request factory to useSimpleClientHttpRequestFactory,
HttpComponentsClientHttpRequestFactorypublic RestTemplate(List<HttpMessageConverter<?>> messageConverters)
RestTemplate using the given list of
HttpMessageConverter to use.messageConverters - the list of HttpMessageConverter to usepublic void setMessageConverters(List<HttpMessageConverter<?>> messageConverters)
These converters are used to convert from and to HTTP requests and responses.
public List<HttpMessageConverter<?>> getMessageConverters()
The returned List is active and may get appended to.
public void setErrorHandler(ResponseErrorHandler errorHandler)
By default, RestTemplate uses a DefaultResponseErrorHandler.
public ResponseErrorHandler getErrorHandler()
public void setDefaultUriVariables(Map<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)
By default, DefaultUriBuilderFactory is used and for
backwards compatibility, the encoding mode is set to
URI_COMPONENT. As of 5.0.8, prefer
using TEMPLATE_AND_VALUES.
Note: in 5.0 the switch from
DefaultUriTemplateHandler (deprecated in 4.3), as the default to use, to
DefaultUriBuilderFactory brings in a different default for the
parsePath property (switching from false to true).
handler - the URI template handler to usepublic UriTemplateHandler getUriTemplateHandler()
@Nullable public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException
RestOperationsURI Template variables are expanded using the given URI variables, if any.
getForObject in interface RestOperationsurl - the URLresponseType - the type of the return valueuriVariables - the variables to expand the templateRestClientException@Nullable public <T> T getForObject(String url, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
RestOperationsURI Template variables are expanded using the given map.
getForObject in interface RestOperationsurl - the URLresponseType - the type of the return valueuriVariables - the map containing variables for the URI templateRestClientException@Nullable public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException
RestOperationsgetForObject in interface RestOperationsurl - the URLresponseType - the type of the return valueRestClientExceptionpublic <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException
RestOperationsResponseEntity.
URI Template variables are expanded using the given URI variables, if any.
getForEntity in interface RestOperationsurl - the URLresponseType - the type of the return valueuriVariables - the variables to expand the templateRestClientExceptionpublic <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
RestOperationsResponseEntity.
URI Template variables are expanded using the given map.
getForEntity in interface RestOperationsurl - the URLresponseType - the type of the return valueuriVariables - the map containing variables for the URI templateRestClientExceptionpublic <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException
RestOperationsResponseEntity.getForEntity in interface RestOperationsurl - the URLresponseType - the type of the return valueRestClientExceptionpublic HttpHeaders headForHeaders(String url, Object... uriVariables) throws RestClientException
RestOperationsURI Template variables are expanded using the given URI variables, if any.
headForHeaders in interface RestOperationsurl - the URLuriVariables - the variables to expand the templateRestClientExceptionpublic HttpHeaders headForHeaders(String url, Map<String,?> uriVariables) throws RestClientException
RestOperationsURI Template variables are expanded using the given map.
headForHeaders in interface RestOperationsurl - the URLuriVariables - the map containing variables for the URI templateRestClientExceptionpublic HttpHeaders headForHeaders(URI url) throws RestClientException
RestOperationsheadForHeaders in interface RestOperationsurl - the URLRestClientException@Nullable public URI postForLocation(String url, @Nullable Object request, Object... uriVariables) throws RestClientException
RestOperationsLocation 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.
The body of the entity, or request itself, can be a
MultiValueMap to create a multipart request.
The values in the MultiValueMap can be any Object representing the body of the part,
or an HttpEntity representing a part with body
and headers.
postForLocation in interface RestOperationsurl - the URLrequest - the Object to be POSTed (may be null)uriVariables - the variables to expand the templateLocation headerRestClientExceptionHttpEntity@Nullable public URI postForLocation(String url, @Nullable Object request, Map<String,?> uriVariables) throws RestClientException
RestOperationsLocation 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
The body of the entity, or request itself, can be a
MultiValueMap to create a multipart request.
The values in the MultiValueMap can be any Object representing the body of the part,
or an HttpEntity representing a part with body
and headers.
postForLocation in interface RestOperationsurl - the URLrequest - the Object to be POSTed (may be null)uriVariables - the variables to expand the templateLocation headerRestClientExceptionHttpEntity@Nullable public URI postForLocation(URI url, @Nullable Object request) throws RestClientException
RestOperationsLocation 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.
The body of the entity, or request itself, can be a
MultiValueMap to create a multipart request.
The values in the MultiValueMap can be any Object representing the body of the part,
or an HttpEntity representing a part with body
and headers.
postForLocation in interface RestOperationsurl - the URLrequest - the Object to be POSTed (may be null)Location headerRestClientExceptionHttpEntity@Nullable public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException
RestOperationsURI 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.
The body of the entity, or request itself, can be a
MultiValueMap to create a multipart request.
The values in the MultiValueMap can be any Object representing the body of the part,
or an HttpEntity representing a part with body
and headers.
postForObject in interface RestOperationsurl - the URLrequest - the Object to be POSTed (may be null)responseType - the type of the return valueuriVariables - the variables to expand the templateRestClientExceptionHttpEntity@Nullable public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
RestOperationsURI 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.
The body of the entity, or request itself, can be a
MultiValueMap to create a multipart request.
The values in the MultiValueMap can be any Object representing the body of the part,
or an HttpEntity representing a part with body
and headers.
postForObject in interface RestOperationsurl - the URLrequest - the Object to be POSTed (may be null)responseType - the type of the return valueuriVariables - the variables to expand the templateRestClientExceptionHttpEntity@Nullable public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException
RestOperationsThe request parameter can be a HttpEntity in order to
add additional HTTP headers to the request.
The body of the entity, or request itself, can be a
MultiValueMap to create a multipart request.
The values in the MultiValueMap can be any Object representing the body of the part,
or an HttpEntity representing a part with body
and headers.
postForObject in interface RestOperationsurl - the URLrequest - the Object to be POSTed (may be null)responseType - the type of the return valueRestClientExceptionHttpEntitypublic <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException
RestOperationsResponseEntity.
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.
The body of the entity, or request itself, can be a
MultiValueMap to create a multipart request.
The values in the MultiValueMap can be any Object representing the body of the part,
or an HttpEntity representing a part with body
and headers.
postForEntity in interface RestOperationsurl - the URLrequest - the Object to be POSTed (may be null)uriVariables - the variables to expand the templateRestClientExceptionHttpEntitypublic <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
RestOperationsHttpEntity.
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.
The body of the entity, or request itself, can be a
MultiValueMap to create a multipart request.
The values in the MultiValueMap can be any Object representing the body of the part,
or an HttpEntity representing a part with body
and headers.
postForEntity in interface RestOperationsurl - the URLrequest - the Object to be POSTed (may be null)uriVariables - the variables to expand the templateRestClientExceptionHttpEntitypublic <T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException
RestOperationsResponseEntity.
The request parameter can be a HttpEntity in order to
add additional HTTP headers to the request.
The body of the entity, or request itself, can be a
MultiValueMap to create a multipart request.
The values in the MultiValueMap can be any Object representing the body of the part,
or an HttpEntity representing a part with body
and headers.
postForEntity in interface RestOperationsurl - the URLrequest - the Object to be POSTed (may be null)RestClientExceptionHttpEntitypublic void put(String url, @Nullable Object request, Object... uriVariables) throws RestClientException
RestOperationsURI 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 RestOperationsurl - the URLrequest - the Object to be PUT (may be null)uriVariables - the variables to expand the templateRestClientExceptionHttpEntitypublic void put(String url, @Nullable Object request, Map<String,?> uriVariables) throws RestClientException
RestOperationsURI 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 RestOperationsurl - the URLrequest - the Object to be PUT (may be null)uriVariables - the variables to expand the templateRestClientExceptionHttpEntitypublic void put(URI url, @Nullable Object request) throws RestClientException
RestOperationsThe request parameter can be a HttpEntity in order to
add additional HTTP headers to the request.
put in interface RestOperationsurl - the URLrequest - the Object to be PUT (may be null)RestClientExceptionHttpEntity@Nullable public <T> T patchForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException
RestOperationsURI 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 RestOperationsurl - the URLrequest - the object to be PATCHed (may be null)responseType - the type of the return valueuriVariables - the variables to expand the templateRestClientExceptionHttpEntity,
InterceptingHttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory),
HttpComponentsAsyncClientHttpRequestFactory,
OkHttp3ClientHttpRequestFactory@Nullable public <T> T patchForObject(String url, @Nullable Object request, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
RestOperationsURI 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 RestOperationsurl - the URLrequest - the object to be PATCHed (may be null)responseType - the type of the return valueuriVariables - the variables to expand the templateRestClientExceptionHttpEntity,
InterceptingHttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory),
HttpComponentsAsyncClientHttpRequestFactory,
OkHttp3ClientHttpRequestFactory@Nullable public <T> T patchForObject(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException
RestOperationsThe 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 RestOperationsurl - the URLrequest - the object to be PATCHed (may be null)responseType - the type of the return valueRestClientExceptionHttpEntity,
InterceptingHttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory),
HttpComponentsAsyncClientHttpRequestFactory,
OkHttp3ClientHttpRequestFactorypublic void delete(String url, Object... uriVariables) throws RestClientException
RestOperationsURI Template variables are expanded using the given URI variables, if any.
delete in interface RestOperationsurl - the URLuriVariables - the variables to expand in the templateRestClientExceptionpublic void delete(String url, Map<String,?> uriVariables) throws RestClientException
RestOperationsURI Template variables are expanded using the given map.
delete in interface RestOperationsurl - the URLuriVariables - the variables to expand the templateRestClientExceptionpublic void delete(URI url) throws RestClientException
RestOperationsdelete in interface RestOperationsurl - the URLRestClientExceptionpublic Set<HttpMethod> optionsForAllow(String url, Object... uriVariables) throws RestClientException
RestOperationsURI Template variables are expanded using the given URI variables, if any.
optionsForAllow in interface RestOperationsurl - the URLuriVariables - the variables to expand in the templateRestClientExceptionpublic Set<HttpMethod> optionsForAllow(String url, Map<String,?> uriVariables) throws RestClientException
RestOperationsURI Template variables are expanded using the given map.
optionsForAllow in interface RestOperationsurl - the URLuriVariables - the variables to expand in the templateRestClientExceptionpublic Set<HttpMethod> optionsForAllow(URI url) throws RestClientException
RestOperationsoptionsForAllow in interface RestOperationsurl - the URLRestClientExceptionpublic <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException
RestOperationsResponseEntity.
URI Template variables are expanded using the given URI variables, if any.
exchange in interface RestOperationsurl - 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 to convert the response to, or Void.class for no bodyuriVariables - the variables to expand in the templateRestClientExceptionpublic <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
RestOperationsResponseEntity.
URI Template variables are expanded using the given URI variables, if any.
exchange in interface RestOperationsurl - 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 to convert the response to, or Void.class for no bodyuriVariables - the variables to expand in the templateRestClientExceptionpublic <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType) throws RestClientException
RestOperationsResponseEntity.exchange in interface RestOperationsurl - 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 to convert the response to, or Void.class for no bodyRestClientExceptionpublic <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Object... uriVariables) throws RestClientException
RestOperationsResponseEntity.
The given ParameterizedTypeReference 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);
exchange in interface RestOperationsurl - 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 to convert the response to, or Void.class for no bodyuriVariables - the variables to expand in the templateRestClientExceptionpublic <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String,?> uriVariables) throws RestClientException
RestOperationsResponseEntity.
The given ParameterizedTypeReference 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);
exchange in interface RestOperationsurl - 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 to convert the response to, or Void.class for no bodyuriVariables - the variables to expand in the templateRestClientExceptionpublic <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException
RestOperationsResponseEntity.
The given ParameterizedTypeReference 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);
exchange in interface RestOperationsurl - 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 to convert the response to, or Void.class for no bodyRestClientExceptionpublic <T> ResponseEntity<T> exchange(RequestEntity<?> entity, Class<T> responseType) throws RestClientException
RestOperationsRequestEntity 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("https://example.com/foo"))
.accept(MediaType.APPLICATION_JSON)
.body(body);
ResponseEntity<MyResponse> response = template.exchange(request, MyResponse.class);
exchange in interface RestOperationsentity - the entity to write to the requestresponseType - the type to convert the response to, or Void.class for no bodyRestClientExceptionpublic <T> ResponseEntity<T> exchange(RequestEntity<?> entity, ParameterizedTypeReference<T> responseType) throws RestClientException
RestOperationsRequestEntity and return
the response as ResponseEntity. The given
ParameterizedTypeReference 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);
exchange in interface RestOperationsentity - the entity to write to the requestresponseType - the type to convert the response to, or Void.class for no bodyRestClientException@Nullable public <T> T execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, Object... uriVariables) throws RestClientException
RequestCallback, and reading the response with a ResponseExtractor.
URI Template variables are expanded using the given URI variables, if any.
To provide a RequestCallback or ResponseExtractor only,
but not both, consider using:
execute in interface RestOperationsurl - 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 templateResponseExtractorRestClientException@Nullable public <T> T execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, Map<String,?> uriVariables) throws RestClientException
RequestCallback, and reading the response with a ResponseExtractor.
URI Template variables are expanded using the given URI variables map.
To provide a RequestCallback or ResponseExtractor only,
but not both, consider using:
execute in interface RestOperationsurl - 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 templateResponseExtractorRestClientException@Nullable public <T> T execute(URI url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) throws RestClientException
RequestCallback, and reading the response with a ResponseExtractor.
To provide a RequestCallback or ResponseExtractor only,
but not both, consider using:
execute in interface RestOperationsurl - the URLmethod - the HTTP method (GET, POST, etc)requestCallback - object that prepares the requestresponseExtractor - object that extracts the return value from the responseResponseExtractorRestClientException@Nullable protected <T> T doExecute(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)ResponseExtractorRestClientExceptionprotected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws 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 ClientHttpResponseIOException - if propagated from ResponseErrorHandlersetErrorHandler(org.springframework.web.client.ResponseErrorHandler)public <T> RequestCallback acceptHeaderRequestCallback(Class<T> responseType)
RequestCallback that sets the request Accept
header based on the given response type, cross-checked against the
configured message converters.public <T> RequestCallback httpEntityCallback(@Nullable Object requestBody)
RequestCallback implementation that writes the given
object to the request stream.public <T> RequestCallback httpEntityCallback(@Nullable Object requestBody, Type responseType)
RequestCallback implementation that:
Accept header based on the given response
type, cross-checked against the configured message converters.
public <T> ResponseExtractor<ResponseEntity<T>> responseEntityExtractor(Type responseType)
ResponseExtractor that prepares a ResponseEntity.protected ResponseExtractor<HttpHeaders> headersExtractor()
HttpHeaders.