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.
Request callback implementation that prepares the request's accept headers. |
| private static class | RestTemplate.
Response extractor that extracts the response |
| private class | RestTemplate.
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. Delete the resources at the specified URI. |
| public void | delete(String
the URL url, Map<String, ?> the variables to expand the template uriVariables)Implements org. Delete the resources at the specified URI. |
| public void | delete(URI
the URL url)Implements org. Delete the resources at the specified URL. |
| 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
doExecute(URI, String, HttpMethod, RequestCallback, ResponseExtractor)
Execute the given method on the provided URI. |
| 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. Execute the HTTP method to the given URI template, writing the given request entity to the request,
and return the response as |
| 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. Execute the HTTP method to the given URI template, writing the given request entity to the request,
and return the response as |
| 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. Execute the HTTP method to the given URI template, writing the given request entity to the request,
and return the response as |
| 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. Execute the HTTP method to the given URI template, writing the given
request entity to the request, and return the response as |
| 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. Execute the HTTP method to the given URI template, writing the given
request entity to the request, and return the response as |
| 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. Execute the HTTP method to the given URI template, writing the given
request entity to the request, and return the response as |
| 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. Execute the request specified in the given |
| 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. Execute the request specified in the given |
| 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. Execute the HTTP method to the given URI template, preparing the request with the
|
| 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. Execute the HTTP method to the given URI template, preparing the request with the
|
| 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. Execute the HTTP method to the given URL, preparing the request with the
|
| 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. Retrieve an entity by doing a GET on the specified URL. |
| 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. Retrieve a representation by doing a GET on the URI template. |
| public <T> ResponseEntity | getForEntity(URI
the URL url, Class<T> the type of the return value responseType)Implements org. Retrieve a representation by doing a GET on the URL. |
| 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. Retrieve a representation by doing a GET on the specified URL. |
| 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. Retrieve a representation by doing a GET on the URI template. |
| public <T> T | getForObject(URI
the URL url, Class<T> the type of the return value responseType)Implements org. Retrieve a representation by doing a GET on the URL. |
| 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. Retrieve all headers of the resource specified by the URI template. |
| public HttpHeaders | headForHeaders(String
the URL url, Map<String, ?> the map containing variables for the URI template uriVariables)Implements org. Retrieve all headers of the resource specified by the URI template. |
| public HttpHeaders | headForHeaders(URI
the URL url)Implements org. Retrieve all headers of the resource specified by the URL. |
| 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. Return the value of the |
| public Set | optionsForAllow(String
the URL url, Map<String, ?> the variables to expand in the template uriVariables)Implements org. Return the value of the |
| public Set | optionsForAllow(URI
the URL url)Implements org. Return the value of the |
| 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. Update a resource by PATCHing the given object to the URI template, and return the representation found in the response. |
| 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. Update a resource by PATCHing the given object to the URI template, and return the representation found in the response. |
| 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. Update a resource by PATCHing the given object to the URL, and return the representation found in the response. |
| 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. Create a new resource by POSTing the given object to the URI template,
and return the response as |
| 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. Create a new resource by POSTing the given object to the URI template,
and return the response as |
| public <T> ResponseEntity | postForEntity(URI
the URL url, Object the Object to be POSTed (may be request, Class<T> responseType)null)Implements org. Create a new resource by POSTing the given object to the URL,
and return the response as |
| 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. Create a new resource by POSTing the given object to the URI template, and return the value of
the |
| 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. Create a new resource by POSTing the given object to the URI template, and return the value of
the |
| public URI | postForLocation(URI
the URL url, Object the Object to be POSTed (may be request)null)Implements org. Create a new resource by POSTing the given object to the URL, and return the value of the
|
| 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. Create a new resource by POSTing the given object to the URI template, and return the representation found in the response. |
| 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. Create a new resource by POSTing the given object to the URI template, and return the representation found in the response. |
| 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. Create a new resource by POSTing the given object to the URL, and return the representation found in the response. |
| 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. Create or update a resource by PUTting the given object to the URI. |
| public void | |
| public void | put(URI
the URL url, Object the Object to be PUT (may be request)null)Implements org. Creates a new resource by PUTting the given object to URL. |
| 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 |