HttpURLConnection, Apache HttpComponents, and others. RestTemplate
offers templates for common scenarios by HTTP method, in addition to the
generalized exchange and execute methods that support
less frequent cases.
RestTemplate is typically used as a shared component. However, its
configuration does not support concurrent modification, and as such its
configuration is typically prepared on startup. If necessary, you can create
multiple, differently configured RestTemplate instances on startup. Such
instances may use the same underlying ClientHttpRequestFactory
if they need to share HTTP client resources.
Note
As of 6.1, RestClient offers a more modern
API for synchronous HTTP access. For asynchronous and streaming scenarios,
consider the reactive
org..
RestTemplate and RestClient share the same infrastructure
(i.e. request factories, request
interceptors and
initializers,
message converters,
etc.), so any improvements made therein are shared as well.
However, RestClient is the focus for new higher-level features.
HttpMessageConverter, RequestCallback, ResponseExtractor, ResponseErrorHandler
| Modifier and Type | Class and Description |
|---|---|
| private class | RestTemplate.AcceptHeaderRequestCallback
Request callback implementation that prepares the request's accept headers. |
| private static class | RestTemplate.HeadersExtractor
Response extractor that extracts the response |
| private class | RestTemplate.HttpEntityRequestCallback
Request callback implementation that writes the given object to the request stream. |
| private class |
| Modifier and Type | Field and Description |
|---|---|
| private static final ClientRequestObservationConvention | |
| private ResponseErrorHandler | |
| private static final boolean | |
| private final ResponseExtractor | |
| private static final boolean | |
| private static final boolean | |
| private static final boolean | |
| private static final boolean | |
| private static final boolean | |
| private static final boolean | |
| private static final boolean | |
| private static final boolean | |
| private static final boolean | |
| private final List | |
| private ClientRequestObservationConvention | |
| private ObservationRegistry | |
| private static final boolean | |
| private UriTemplateHandler |
| Access | Constructor and Description |
|---|---|
| public | |
| public | RestTemplate(ClientHttpRequestFactory
the HTTP request factory to use requestFactory)Create a new instance of the |
| public | RestTemplate(List<HttpMessageConverter<?>>
the list of messageConverters)HttpMessageConverter to useCreate a new instance of the |
| Modifier and Type | Method and Description |
|---|---|
| public <T> RequestCallback | acceptHeaderRequestCallback(Class<T> responseType)
Return a |
| private static ResourceAccessException | |
| public void | delete(String
the URL url, Object... the variables to expand in the template uriVariables)Implements org. |
| public void | delete(String
the URL url, Map<String, ?> the variables to expand the template uriVariables)Implements org. |
| public void | delete(URI
the URL url)Implements org. |
| protected <T> T | Returns: an arbitrary object, as returned by theResponseExtractorthe fully-expanded URL to connect to url, HttpMethod the HTTP method to execute (GET, POST, etc.) method, RequestCallback object that prepares the request (can be requestCallback, ResponseExtractor<T> null)object that extracts the return value from the response (can be responseExtractor)null)
Deprecated
in favor of
Execute the given method on the provided URI.
doExecute(URI, String, HttpMethod, RequestCallback, ResponseExtractor)
|
| protected <T> T | Returns: an arbitrary object, as returned by theResponseExtractorthe fully-expanded URL to connect to url, String the URI template that was used for creating the expanded URL uriTemplate, HttpMethod the HTTP method to execute (GET, POST, etc.) method, RequestCallback object that prepares the request (can be requestCallback, ResponseExtractor<T> null)object that extracts the return value from the response (can be responseExtractor)null)Execute the given method on the provided URI. |
| public <T> ResponseEntity | exchange(String
the URL url, HttpMethod the HTTP method (GET, POST, etc) method, HttpEntity<?> the entity (headers and/or body) to write to the request
may be requestEntity, Class<T> null)the type to convert the response to, or responseType, Object... Void.class for no bodythe variables to expand in the template uriVariables)Implements org. ResponseEntity.
|
| public <T> ResponseEntity | exchange(String
the URL url, HttpMethod the HTTP method (GET, POST, etc) method, HttpEntity<?> the entity (headers and/or body) to write to the request
(may be requestEntity, Class<T> null)the type to convert the response to, or responseType, Map<String, ?> Void.class for no bodythe variables to expand in the template uriVariables)Implements org. ResponseEntity.
|
| public <T> ResponseEntity | exchange(URI
the URL url, HttpMethod the HTTP method (GET, POST, etc) method, HttpEntity<?> the entity (headers and/or body) to write to the request
(may be requestEntity, Class<T> null)the type to convert the response to, or responseType)Void.class for no bodyImplements org. ResponseEntity.
|
| public <T> ResponseEntity | exchange(String
the URL url, HttpMethod the HTTP method (GET, POST, etc) method, HttpEntity<?> the entity (headers and/or body) to write to the
request (may be requestEntity, ParameterizedTypeReference<T> null)the type to convert the response to, or responseType, Object... Void.class for no bodythe variables to expand in the template uriVariables)Implements org. ResponseEntity.
|
| public <T> ResponseEntity | exchange(String
the URL url, HttpMethod the HTTP method (GET, POST, etc) method, HttpEntity<?> the entity (headers and/or body) to write to the request
(may be requestEntity, ParameterizedTypeReference<T> null)the type to convert the response to, or responseType, Map<String, ?> Void.class for no bodythe variables to expand in the template uriVariables)Implements org. ResponseEntity.
|
| public <T> ResponseEntity | exchange(URI
the URL url, HttpMethod the HTTP method (GET, POST, etc) method, HttpEntity<?> the entity (headers and/or body) to write to the request
(may be requestEntity, ParameterizedTypeReference<T> null)the type to convert the response to, or responseType)Void.class for no bodyImplements org. ResponseEntity.
|
| public <T> ResponseEntity | exchange(RequestEntity<?>
the entity to write to the request entity, Class<T> the type to convert the response to, or responseType)Void.class for no bodyImplements org. RequestEntity and return
the response as ResponseEntity.
|
| public <T> ResponseEntity | exchange(RequestEntity<?>
the entity to write to the request entity, ParameterizedTypeReference<T> the type to convert the response to, or responseType)Void.class for no bodyImplements org. RequestEntity and return
the response as ResponseEntity.
|
| public <T> T | execute(String
the URI template uriTemplate, HttpMethod the HTTP method (GET, POST, etc) method, RequestCallback object that prepares the request requestCallback, ResponseExtractor<T> object that extracts the return value from the response responseExtractor, Object... the variables to expand in the template uriVariables)Implements org. RequestCallback, and reading the response with a ResponseExtractor.
|
| public <T> T | execute(String
the URI template uriTemplate, HttpMethod the HTTP method (GET, POST, etc) method, RequestCallback object that prepares the request requestCallback, ResponseExtractor<T> object that extracts the return value from the response responseExtractor, Map<String, ?> the variables to expand in the template uriVariables)Implements org. RequestCallback, and reading the response with a ResponseExtractor.
|
| public <T> T | execute(URI
the URL url, HttpMethod the HTTP method (GET, POST, etc) method, RequestCallback object that prepares the request requestCallback, ResponseExtractor<T> object that extracts the return value from the response responseExtractor)Implements org. RequestCallback, and reading the response with a ResponseExtractor.
|
| public ResponseErrorHandler | |
| public <T> ResponseEntity | getForEntity(String
the URL url, Class<T> the type of the return value responseType, Object... the variables to expand the template uriVariables)Implements org. |
| public <T> ResponseEntity | getForEntity(String
the URL url, Class<T> the type of the return value responseType, Map<String, ?> the map containing variables for the URI template uriVariables)Implements org. |
| public <T> ResponseEntity | getForEntity(URI
the URL url, Class<T> the type of the return value responseType)Implements org. |
| public <T> T | getForObject(String
the URL url, Class<T> the type of the return value responseType, Object... the variables to expand the template uriVariables)Implements org. |
| public <T> T | getForObject(String
the URL url, Class<T> the type of the return value responseType, Map<String, ?> the map containing variables for the URI template uriVariables)Implements org. |
| public <T> T | getForObject(URI
the URL url, Class<T> the type of the return value responseType)Implements org. |
| public List | |
| public ClientRequestObservationConvention | getObservationConvention()
Return the configured |
| public ObservationRegistry | |
| public UriTemplateHandler | |
| protected void | handleResponse(URI
the fully-expanded URL to connect to url, HttpMethod the HTTP method to execute (GET, POST, etc.) method, ClientHttpResponse the resulting response)ClientHttpResponseHandle the given response, performing appropriate logging and
invoking the |
| protected ResponseExtractor | |
| public HttpHeaders | headForHeaders(String
the URL url, Object... the variables to expand the template uriVariables)Implements org. |
| public HttpHeaders | headForHeaders(String
the URL url, Map<String, ?> the map containing variables for the URI template uriVariables)Implements org. |
| public HttpHeaders | headForHeaders(URI
the URL url)Implements org. |
| public <T> RequestCallback | httpEntityCallback(Object requestBody)
Return a |
| public <T> RequestCallback | httpEntityCallback(Object requestBody, Type responseType)
Return a
|
| private static DefaultUriBuilderFactory | |
| private static <T> T | |
| public Set | optionsForAllow(String
the URL url, Object... the variables to expand in the template uriVariables)Implements org. Allow header for the given URI.
|
| public Set | optionsForAllow(String
the URL url, Map<String, ?> the variables to expand in the template uriVariables)Implements org. Allow header for the given URI.
|
| public Set | optionsForAllow(URI
the URL url)Implements org. Allow header for the given URL.
|
| public <T> T | patchForObject(String
the URL url, Object the object to be PATCHed (may be request, Class<T> null)the type of the return value responseType, Object... the variables to expand the template uriVariables)Implements org. |
| public <T> T | patchForObject(String
the URL url, Object the object to be PATCHed (may be request, Class<T> null)the type of the return value responseType, Map<String, ?> the variables to expand the template uriVariables)Implements org. |
| public <T> T | patchForObject(URI
the URL url, Object the object to be PATCHed (may be request, Class<T> null)the type of the return value responseType)Implements org. |
| public <T> ResponseEntity | postForEntity(String
the URL url, Object the Object to be POSTed (may be request, Class<T> responseType, Object... null)the variables to expand the template uriVariables)Implements org. ResponseEntity.
|
| public <T> ResponseEntity | postForEntity(String
the URL url, Object the Object to be POSTed (may be request, Class<T> responseType, Map<String, ?> null)the variables to expand the template uriVariables)Implements org. HttpEntity.
|
| public <T> ResponseEntity | postForEntity(URI
the URL url, Object the Object to be POSTed (may be request, Class<T> responseType)null)Implements org. ResponseEntity.
|
| public URI | postForLocation(String
the URL url, Object the Object to be POSTed (may be request, Object... null)the variables to expand the template uriVariables)Implements org. Location header.
|
| public URI | postForLocation(String
the URL url, Object the Object to be POSTed (may be request, Map<String, ?> null)the variables to expand the template uriVariables)Implements org. Location header.
|
| public URI | postForLocation(URI
the URL url, Object the Object to be POSTed (may be request)null)Implements org. Location header.
|
| public <T> T | postForObject(String
the URL url, Object the Object to be POSTed (may be request, Class<T> null)the type of the return value responseType, Object... the variables to expand the template uriVariables)Implements org. |
| public <T> T | postForObject(String
the URL url, Object the Object to be POSTed (may be request, Class<T> null)the type of the return value responseType, Map<String, ?> the variables to expand the template uriVariables)Implements org. |
| public <T> T | postForObject(URI
the URL url, Object the Object to be POSTed (may be request, Class<T> null)the type of the return value responseType)Implements org. |
| public void | put(String
the URL url, Object the Object to be PUT (may be request, Object... null)the variables to expand the template uriVariables)Implements org. |
| public void | |
| public void | put(URI
the URL url, Object the Object to be PUT (may be request)null)Implements org. |
| private String | |
| private URI | |
| public <T> ResponseExtractor | responseEntityExtractor(Type responseType)
Return a |
| public void | setDefaultUriVariables(Map<String, ?>
the default URI variable values uriVars)Configure default URI variable values. |
| public void | |
| public void | setMessageConverters(List<HttpMessageConverter<?>> messageConverters)
Set the message body converters to use. |
| public void | setObservationConvention(ClientRequestObservationConvention
the observation convention to use observationConvention)Configure an |
| public void | setObservationRegistry(ObservationRegistry
the observation registry to use observationRegistry)Configure an |
| public void | setUriTemplateHandler(UriTemplateHandler
the URI template handler to use handler)Configure a strategy for expanding URI templates. |
| private void | |
| private void |