Class RestTemplate
- All Implemented Interfaces:
- RestOperations
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
 WebClient.
 
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.
- Since:
- 3.0
- Author:
- Arjen Poutsma, Brian Clozel, Roy Clarkson, Juergen Hoeller, Sam Brannen, Sebastien Deleuze, Hyoungjune Kim
- See Also:
- 
Field SummaryFields inherited from class org.springframework.http.client.support.HttpAccessorlogger
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a new instance with default settings.RestTemplate(List<HttpMessageConverter<?>> messageConverters) Create a new instance with the given message converters.RestTemplate(ClientHttpRequestFactory requestFactory) Create a new instance with the givenClientHttpRequestFactory.
- 
Method SummaryModifier and TypeMethodDescription<T> RequestCallbackacceptHeaderRequestCallback(Class<T> responseType) Return aRequestCallbackthat sets the requestAcceptheader based on the given response type, cross-checked against the configured message converters.voidDelete the resources at the specified URI.voidDelete the resources at the specified URI.voidDelete the resources at the specified URL.protected <T> @Nullable TdoExecute(URI url, @Nullable String uriTemplate, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) Execute the given method on the provided URI.<T> ResponseEntity<T>exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, @Nullable Object... uriVariables) Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response asResponseEntity.<T> ResponseEntity<T>exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ? extends @Nullable Object> uriVariables) Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response asResponseEntity.<T> ResponseEntity<T>exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, @Nullable Object... uriVariables) Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response asResponseEntity.<T> ResponseEntity<T>exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String, ? extends @Nullable Object> uriVariables) Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response asResponseEntity.<T> ResponseEntity<T>exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType) Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response asResponseEntity.<T> ResponseEntity<T>exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response asResponseEntity.<T> ResponseEntity<T>exchange(RequestEntity<?> entity, Class<T> responseType) Execute the request specified in the givenRequestEntityand return the response asResponseEntity.<T> ResponseEntity<T>exchange(RequestEntity<?> entity, ParameterizedTypeReference<T> responseType) Execute the request specified in the givenRequestEntityand return the response asResponseEntity.<T> @Nullable Texecute(String uriTemplate, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, @Nullable Object... uriVariables) Execute the HTTP method to the given URI template, preparing the request with theRequestCallback, and reading the response with aResponseExtractor.<T> @Nullable Texecute(String uriTemplate, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, Map<String, ? extends @Nullable Object> uriVariables) Execute the HTTP method to the given URI template, preparing the request with theRequestCallback, and reading the response with aResponseExtractor.<T> @Nullable Texecute(URI url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) Execute the HTTP method to the given URL, preparing the request with theRequestCallback, and reading the response with aResponseExtractor.Return the error handler.<T> ResponseEntity<T>getForEntity(String url, Class<T> responseType, @Nullable Object... uriVariables) Retrieve an entity by doing a GET on the specified URL.<T> ResponseEntity<T>getForEntity(String url, Class<T> responseType, Map<String, ? extends @Nullable Object> uriVariables) Retrieve a representation by doing a GET on the URI template.<T> ResponseEntity<T>getForEntity(URI url, Class<T> responseType) Retrieve a representation by doing a GET on the URL.<T> @Nullable TgetForObject(String url, Class<T> responseType, @Nullable Object... uriVariables) Retrieve a representation by doing a GET on the specified URL.<T> @Nullable TgetForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) Retrieve a representation by doing a GET on the URI template.<T> @Nullable TgetForObject(URI url, Class<T> responseType) Retrieve a representation by doing a GET on the URL.Return the list of message body converters.Return the configuredClientRequestObservationConvention, ornullif not set.io.micrometer.observation.ObservationRegistryReturn the configuredObservationRegistry.Return the configured URI template handler.protected voidhandleResponse(URI url, HttpMethod method, ClientHttpResponse response) Handle the given response, performing appropriate logging and invoking theResponseErrorHandlerif necessary.protected ResponseExtractor<HttpHeaders>Return a response extractor forHttpHeaders.headForHeaders(String url, @Nullable Object... uriVariables) Retrieve all headers of the resource specified by the URI template.Retrieve all headers of the resource specified by the URI template.headForHeaders(URI url) Retrieve all headers of the resource specified by the URL.<T> RequestCallbackhttpEntityCallback(@Nullable Object requestBody) Return aRequestCallbackimplementation that writes the given object to the request stream.<T> RequestCallbackhttpEntityCallback(@Nullable Object requestBody, Type responseType) Return aRequestCallbackimplementation that: Sets the requestAcceptheader based on the given response type, cross-checked against the configured message converters.optionsForAllow(String url, @Nullable Object... uriVariables) Return the value of theAllowheader for the given URI.Return the value of theAllowheader for the given URI.optionsForAllow(URI url) Return the value of theAllowheader for the given URL.<T> @Nullable TpatchForObject(String url, @Nullable Object request, Class<T> responseType, @Nullable Object... uriVariables) Update a resource by PATCHing the given object to the URI template, and return the representation found in the response.<T> @Nullable TpatchForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ? extends @Nullable Object> uriVariables) Update a resource by PATCHing the given object to the URI template, and return the representation found in the response.<T> @Nullable TpatchForObject(URI url, @Nullable 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, @Nullable Object request, Class<T> responseType, @Nullable Object... uriVariables) Create a new resource by POSTing the given object to the URI template, and return the response asResponseEntity.<T> ResponseEntity<T>postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String, ? extends @Nullable Object> uriVariables) Create a new resource by POSTing the given object to the URI template, and return the response asHttpEntity.<T> ResponseEntity<T>postForEntity(URI url, @Nullable Object request, Class<T> responseType) Create a new resource by POSTing the given object to the URL, and return the response asResponseEntity.Create a new resource by POSTing the given object to the URI template, and return the value of theLocationheader.postForLocation(String url, @Nullable Object request, Map<String, ? extends @Nullable Object> uriVariables) Create a new resource by POSTing the given object to the URI template, and return the value of theLocationheader.postForLocation(URI url, @Nullable Object request) Create a new resource by POSTing the given object to the URL, and return the value of theLocationheader.<T> @Nullable TpostForObject(String url, @Nullable Object request, Class<T> responseType, @Nullable Object... uriVariables) Create a new resource by POSTing the given object to the URI template, and return the representation found in the response.<T> @Nullable TpostForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ? extends @Nullable Object> uriVariables) Create a new resource by POSTing the given object to the URI template, and return the representation found in the response.<T> @Nullable TpostForObject(URI url, @Nullable Object request, Class<T> responseType) Create a new resource by POSTing the given object to the URL, and return the representation found in the response.voidCreate or update a resource by PUTting the given object to the URI.voidCreates a new resource by PUTting the given object to URI template.voidCreates a new resource by PUTting the given object to URL.<T> ResponseExtractor<ResponseEntity<T>>responseEntityExtractor(Type responseType) Return aResponseExtractorthat prepares aResponseEntity.voidsetDefaultUriVariables(Map<String, ? extends @Nullable Object> uriVars) Configure default URI variable values.voidsetErrorHandler(ResponseErrorHandler errorHandler) Set the error handler.voidsetMessageConverters(List<HttpMessageConverter<?>> messageConverters) Set the message body converters to use.voidsetObservationConvention(ClientRequestObservationConvention observationConvention) Configure anObservationConventionthat sets the name of theobservationas well as itsKeyValuesextracted from theClientRequestObservationContext.voidsetObservationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry) Configure anObservationRegistryfor collecting spans and metrics for request execution.voidsetUriTemplateHandler(UriTemplateHandler handler) Configure a strategy for expanding URI templates.Methods inherited from class org.springframework.http.client.support.InterceptingHttpAccessorgetInterceptors, getRequestFactory, setInterceptors, setRequestFactoryMethods inherited from class org.springframework.http.client.support.HttpAccessorcreateRequest, getBufferingPredicate, getClientHttpRequestInitializers, setBufferingPredicate, setClientHttpRequestInitializers
- 
Constructor Details- 
RestTemplatepublic RestTemplate()Create a new instance with default settings. DefaultHttpMessageConvertersare initialized.
- 
RestTemplateCreate a new instance with the givenClientHttpRequestFactory.- Parameters:
- requestFactory- the HTTP request factory to use
- See Also:
 
- 
RestTemplateCreate a new instance with the given message converters.- Parameters:
- messageConverters- the list of converters to use
- Since:
- 3.2.7
 
 
- 
- 
Method Details- 
setMessageConvertersSet the message body converters to use.These converters are used to convert from and to HTTP requests and responses. 
- 
getMessageConvertersReturn the list of message body converters.The returned Listis active and may get appended to.
- 
setErrorHandlerSet the error handler.By default, RestTemplate uses a DefaultResponseErrorHandler.
- 
getErrorHandlerReturn the error handler.
- 
setDefaultUriVariablesConfigure default URI variable values. This is a shortcut for:DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(); handler.setDefaultUriVariables(...); RestTemplate restTemplate = new RestTemplate(); restTemplate.setUriTemplateHandler(handler); - Parameters:
- uriVars- the default URI variable values
- Since:
- 4.3
 
- 
setUriTemplateHandlerConfigure a strategy for expanding URI templates.By default, DefaultUriBuilderFactoryis used and for backwards compatibility, the encoding mode is set toURI_COMPONENT. As of 5.0.8, prefer usingTEMPLATE_AND_VALUES.- Parameters:
- handler- the URI template handler to use
 
- 
getUriTemplateHandlerReturn the configured URI template handler.
- 
setObservationRegistrypublic void setObservationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry) Configure anObservationRegistryfor collecting spans and metrics for request execution. By default,observationsare no-ops.- Parameters:
- observationRegistry- the observation registry to use
- Since:
- 6.0
 
- 
getObservationRegistrypublic io.micrometer.observation.ObservationRegistry getObservationRegistry()Return the configuredObservationRegistry.- Since:
- 6.1
 
- 
setObservationConventionConfigure anObservationConventionthat sets the name of theobservationas well as itsKeyValuesextracted from theClientRequestObservationContext. If none set, thedefault conventionwill be used.- Parameters:
- observationConvention- the observation convention to use
- Since:
- 6.0
- See Also:
 
- 
getObservationConventionReturn the configuredClientRequestObservationConvention, ornullif not set.- Since:
- 6.1
 
- 
getForObjectpublic <T> @Nullable T getForObject(String url, Class<T> responseType, @Nullable Object... uriVariables) throws RestClientException Description copied from interface:RestOperationsRetrieve a representation by doing a GET on the specified URL. The response (if any) is converted and returned.URI Template variables are expanded using the given URI variables, if any. - Specified by:
- getForObjectin interface- RestOperations
- Parameters:
- url- the URL
- responseType- the type of the return value
- uriVariables- the variables to expand the template
- Returns:
- the converted object
- Throws:
- RestClientException
 
- 
getForObjectpublic <T> @Nullable T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientExceptionDescription copied from interface:RestOperationsRetrieve a representation by doing a GET on the URI template. The response (if any) is converted and returned.URI Template variables are expanded using the given map. - Specified by:
- getForObjectin interface- RestOperations
- Parameters:
- url- the URL
- responseType- the type of the return value
- uriVariables- the map containing variables for the URI template
- Returns:
- the converted object
- Throws:
- RestClientException
 
- 
getForObjectDescription copied from interface:RestOperationsRetrieve a representation by doing a GET on the URL. The response (if any) is converted and returned.- Specified by:
- getForObjectin interface- RestOperations
- Parameters:
- url- the URL
- responseType- the type of the return value
- Returns:
- the converted object
- Throws:
- RestClientException
 
- 
getForEntitypublic <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, @Nullable Object... uriVariables) throws RestClientException Description copied from interface:RestOperationsRetrieve an entity by doing a GET on the specified URL. The response is converted and stored in aResponseEntity.URI Template variables are expanded using the given URI variables, if any. - Specified by:
- getForEntityin interface- RestOperations
- Parameters:
- url- the URL
- responseType- the type of the return value
- uriVariables- the variables to expand the template
- Returns:
- the entity
- Throws:
- RestClientException
 
- 
getForEntitypublic <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ? extends @Nullable Object> uriVariables) throws RestClientExceptionDescription copied from interface:RestOperationsRetrieve a representation by doing a GET on the URI template. The response is converted and stored in aResponseEntity.URI Template variables are expanded using the given map. - Specified by:
- getForEntityin interface- RestOperations
- Parameters:
- url- the URL
- responseType- the type of the return value
- uriVariables- the map containing variables for the URI template
- Returns:
- the converted object
- Throws:
- RestClientException
 
- 
getForEntitypublic <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException Description copied from interface:RestOperationsRetrieve a representation by doing a GET on the URL. The response is converted and stored in aResponseEntity.- Specified by:
- getForEntityin interface- RestOperations
- Parameters:
- url- the URL
- responseType- the type of the return value
- Returns:
- the converted object
- Throws:
- RestClientException
 
- 
headForHeaderspublic HttpHeaders headForHeaders(String url, @Nullable Object... uriVariables) throws RestClientException Description copied from interface:RestOperationsRetrieve all headers of the resource specified by the URI template.URI Template variables are expanded using the given URI variables, if any. - Specified by:
- headForHeadersin interface- RestOperations
- Parameters:
- url- the URL
- uriVariables- the variables to expand the template
- Returns:
- all HTTP headers of that resource
- Throws:
- RestClientException
 
- 
headForHeaderspublic HttpHeaders headForHeaders(String url, Map<String, ? extends @Nullable Object> uriVariables) throws RestClientExceptionDescription copied from interface:RestOperationsRetrieve all headers of the resource specified by the URI template.URI Template variables are expanded using the given map. - Specified by:
- headForHeadersin interface- RestOperations
- Parameters:
- url- the URL
- uriVariables- the map containing variables for the URI template
- Returns:
- all HTTP headers of that resource
- Throws:
- RestClientException
 
- 
headForHeadersDescription copied from interface:RestOperationsRetrieve all headers of the resource specified by the URL.- Specified by:
- headForHeadersin interface- RestOperations
- Parameters:
- url- the URL
- Returns:
- all HTTP headers of that resource
- Throws:
- RestClientException
 
- 
postForLocationpublic @Nullable URI postForLocation(String url, @Nullable Object request, @Nullable Object... uriVariables) throws RestClientException Description copied from interface:RestOperationsCreate a new resource by POSTing the given object to the URI template, and return the value of theLocationheader. This header typically indicates where the new resource is stored.URI Template variables are expanded using the given URI variables, if any. The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.The body of the entity, or requestitself, can be aMultiValueMapto create a multipart request. The values in theMultiValueMapcan be any Object representing the body of the part, or anHttpEntityrepresenting a part with body and headers.- Specified by:
- postForLocationin interface- RestOperations
- Parameters:
- url- the URL
- request- the Object to be POSTed (may be- null)
- uriVariables- the variables to expand the template
- Returns:
- the value for the Locationheader
- Throws:
- RestClientException
- See Also:
 
- 
postForLocationpublic @Nullable URI postForLocation(String url, @Nullable Object request, Map<String, ? extends @Nullable Object> uriVariables) throws RestClientExceptionDescription copied from interface:RestOperationsCreate a new resource by POSTing the given object to the URI template, and return the value of theLocationheader. This header typically indicates where the new resource is stored.URI Template variables are expanded using the given map. The requestparameter can be aHttpEntityin order to add additional HTTP headers to the requestThe body of the entity, or requestitself, can be aMultiValueMapto create a multipart request. The values in theMultiValueMapcan be any Object representing the body of the part, or anHttpEntityrepresenting a part with body and headers.- Specified by:
- postForLocationin interface- RestOperations
- Parameters:
- url- the URL
- request- the Object to be POSTed (may be- null)
- uriVariables- the variables to expand the template
- Returns:
- the value for the Locationheader
- Throws:
- RestClientException
- See Also:
 
- 
postForLocationDescription copied from interface:RestOperationsCreate a new resource by POSTing the given object to the URL, and return the value of theLocationheader. This header typically indicates where the new resource is stored.The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.The body of the entity, or requestitself, can be aMultiValueMapto create a multipart request. The values in theMultiValueMapcan be any Object representing the body of the part, or anHttpEntityrepresenting a part with body and headers.- Specified by:
- postForLocationin interface- RestOperations
- Parameters:
- url- the URL
- request- the Object to be POSTed (may be- null)
- Returns:
- the value for the Locationheader
- Throws:
- RestClientException
- See Also:
 
- 
postForObjectpublic <T> @Nullable T postForObject(String url, @Nullable Object request, Class<T> responseType, @Nullable Object... uriVariables) throws RestClientException Description copied from interface:RestOperationsCreate a new resource by POSTing the given object to the URI template, and return the representation found in the response.URI Template variables are expanded using the given URI variables, if any. The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.The body of the entity, or requestitself, can be aMultiValueMapto create a multipart request. The values in theMultiValueMapcan be any Object representing the body of the part, or anHttpEntityrepresenting a part with body and headers.- Specified by:
- postForObjectin interface- RestOperations
- Parameters:
- url- the URL
- request- the Object to be POSTed (may be- null)
- responseType- the type of the return value
- uriVariables- the variables to expand the template
- Returns:
- the converted object
- Throws:
- RestClientException
- See Also:
 
- 
postForObjectpublic <T> @Nullable T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ? extends @Nullable Object> uriVariables) throws RestClientExceptionDescription copied from interface:RestOperationsCreate a new resource by POSTing the given object to the URI template, and return the representation found in the response.URI Template variables are expanded using the given map. The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.The body of the entity, or requestitself, can be aMultiValueMapto create a multipart request. The values in theMultiValueMapcan be any Object representing the body of the part, or anHttpEntityrepresenting a part with body and headers.- Specified by:
- postForObjectin interface- RestOperations
- Parameters:
- url- the URL
- request- the Object to be POSTed (may be- null)
- responseType- the type of the return value
- uriVariables- the variables to expand the template
- Returns:
- the converted object
- Throws:
- RestClientException
- See Also:
 
- 
postForObjectpublic <T> @Nullable T postForObject(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException Description copied from interface:RestOperationsCreate a new resource by POSTing the given object to the URL, and return the representation found in the response.The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.The body of the entity, or requestitself, can be aMultiValueMapto create a multipart request. The values in theMultiValueMapcan be any Object representing the body of the part, or anHttpEntityrepresenting a part with body and headers.- Specified by:
- postForObjectin interface- RestOperations
- Parameters:
- url- the URL
- request- the Object to be POSTed (may be- null)
- responseType- the type of the return value
- Returns:
- the converted object
- Throws:
- RestClientException
- See Also:
 
- 
postForEntitypublic <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, @Nullable Object... uriVariables) throws RestClientException Description copied from interface:RestOperationsCreate a new resource by POSTing the given object to the URI template, and return the response asResponseEntity.URI Template variables are expanded using the given URI variables, if any. The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.The body of the entity, or requestitself, can be aMultiValueMapto create a multipart request. The values in theMultiValueMapcan be any Object representing the body of the part, or anHttpEntityrepresenting a part with body and headers.- Specified by:
- postForEntityin interface- RestOperations
- Parameters:
- url- the URL
- request- the Object to be POSTed (may be- null)
- uriVariables- the variables to expand the template
- Returns:
- the converted object
- Throws:
- RestClientException
- See Also:
 
- 
postForEntitypublic <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String, ? extends @Nullable Object> uriVariables) throws RestClientExceptionDescription copied from interface:RestOperationsCreate a new resource by POSTing the given object to the URI template, and return the response asHttpEntity.URI Template variables are expanded using the given map. The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.The body of the entity, or requestitself, can be aMultiValueMapto create a multipart request. The values in theMultiValueMapcan be any Object representing the body of the part, or anHttpEntityrepresenting a part with body and headers.- Specified by:
- postForEntityin interface- RestOperations
- Parameters:
- url- the URL
- request- the Object to be POSTed (may be- null)
- uriVariables- the variables to expand the template
- Returns:
- the converted object
- Throws:
- RestClientException
- See Also:
 
- 
postForEntitypublic <T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException Description copied from interface:RestOperationsCreate a new resource by POSTing the given object to the URL, and return the response asResponseEntity.The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.The body of the entity, or requestitself, can be aMultiValueMapto create a multipart request. The values in theMultiValueMapcan be any Object representing the body of the part, or anHttpEntityrepresenting a part with body and headers.- Specified by:
- postForEntityin interface- RestOperations
- Parameters:
- url- the URL
- request- the Object to be POSTed (may be- null)
- Returns:
- the converted object
- Throws:
- RestClientException
- See Also:
 
- 
putpublic void put(String url, @Nullable Object request, @Nullable Object... uriVariables) throws RestClientException Description copied from interface:RestOperationsCreate or update a resource by PUTting the given object to the URI.URI Template variables are expanded using the given URI variables, if any. The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- Specified by:
- putin interface- RestOperations
- Parameters:
- url- the URL
- request- the Object to be PUT (may be- null)
- uriVariables- the variables to expand the template
- Throws:
- RestClientException
- See Also:
 
- 
putpublic void put(String url, @Nullable Object request, Map<String, ? extends @Nullable Object> uriVariables) throws RestClientExceptionDescription copied from interface:RestOperationsCreates a new resource by PUTting the given object to URI template.URI Template variables are expanded using the given map. The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- Specified by:
- putin interface- RestOperations
- Parameters:
- url- the URL
- request- the Object to be PUT (may be- null)
- uriVariables- the variables to expand the template
- Throws:
- RestClientException
- See Also:
 
- 
putDescription copied from interface:RestOperationsCreates a new resource by PUTting the given object to URL.The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- Specified by:
- putin interface- RestOperations
- Parameters:
- url- the URL
- request- the Object to be PUT (may be- null)
- Throws:
- RestClientException
- See Also:
 
- 
patchForObjectpublic <T> @Nullable T patchForObject(String url, @Nullable Object request, Class<T> responseType, @Nullable Object... uriVariables) throws RestClientException Description copied from interface:RestOperationsUpdate a resource by PATCHing the given object to the URI template, and return the representation found in the response.URI Template variables are expanded using the given URI variables, if any. The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.NOTE: The standard JDK HTTP library does not support HTTP PATCH. You need to use, for example, the Apache HttpComponents request factory. - Specified by:
- patchForObjectin interface- RestOperations
- Parameters:
- url- the URL
- request- the object to be PATCHed (may be- null)
- responseType- the type of the return value
- uriVariables- the variables to expand the template
- Returns:
- the converted object
- Throws:
- RestClientException
- See Also:
 
- 
patchForObjectpublic <T> @Nullable T patchForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ? extends @Nullable Object> uriVariables) throws RestClientExceptionDescription copied from interface:RestOperationsUpdate a resource by PATCHing the given object to the URI template, and return the representation found in the response.URI Template variables are expanded using the given map. The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.NOTE: The standard JDK HTTP library does not support HTTP PATCH. You need to use, for example, the Apache HttpComponents request factory. - Specified by:
- patchForObjectin interface- RestOperations
- Parameters:
- url- the URL
- request- the object to be PATCHed (may be- null)
- responseType- the type of the return value
- uriVariables- the variables to expand the template
- Returns:
- the converted object
- Throws:
- RestClientException
- See Also:
 
- 
patchForObjectpublic <T> @Nullable T patchForObject(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException Description copied from interface:RestOperationsUpdate a resource by PATCHing the given object to the URL, and return the representation found in the response.The requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.NOTE: The standard JDK HTTP library does not support HTTP PATCH. You need to use, for example, the Apache HttpComponents request factory. - Specified by:
- patchForObjectin interface- RestOperations
- Parameters:
- url- the URL
- request- the object to be PATCHed (may be- null)
- responseType- the type of the return value
- Returns:
- the converted object
- Throws:
- RestClientException
- See Also:
 
- 
deleteDescription copied from interface:RestOperationsDelete the resources at the specified URI.URI Template variables are expanded using the given URI variables, if any. - Specified by:
- deletein interface- RestOperations
- Parameters:
- url- the URL
- uriVariables- the variables to expand in the template
- Throws:
- RestClientException
 
- 
deletepublic void delete(String url, Map<String, ? extends @Nullable Object> uriVariables) throws RestClientExceptionDescription copied from interface:RestOperationsDelete the resources at the specified URI.URI Template variables are expanded using the given map. - Specified by:
- deletein interface- RestOperations
- Parameters:
- url- the URL
- uriVariables- the variables to expand the template
- Throws:
- RestClientException
 
- 
deleteDescription copied from interface:RestOperationsDelete the resources at the specified URL.- Specified by:
- deletein interface- RestOperations
- Parameters:
- url- the URL
- Throws:
- RestClientException
 
- 
optionsForAllowpublic Set<HttpMethod> optionsForAllow(String url, @Nullable Object... uriVariables) throws RestClientException Description copied from interface:RestOperationsReturn the value of theAllowheader for the given URI.URI Template variables are expanded using the given URI variables, if any. - Specified by:
- optionsForAllowin interface- RestOperations
- Parameters:
- url- the URL
- uriVariables- the variables to expand in the template
- Returns:
- the value of the Allowheader
- Throws:
- RestClientException
 
- 
optionsForAllowpublic Set<HttpMethod> optionsForAllow(String url, Map<String, ? extends @Nullable Object> uriVariables) throws RestClientExceptionDescription copied from interface:RestOperationsReturn the value of theAllowheader for the given URI.URI Template variables are expanded using the given map. - Specified by:
- optionsForAllowin interface- RestOperations
- Parameters:
- url- the URL
- uriVariables- the variables to expand in the template
- Returns:
- the value of the Allowheader
- Throws:
- RestClientException
 
- 
optionsForAllowDescription copied from interface:RestOperationsReturn the value of theAllowheader for the given URL.- Specified by:
- optionsForAllowin interface- RestOperations
- Parameters:
- url- the URL
- Returns:
- the value of the Allowheader
- Throws:
- RestClientException
 
- 
exchangepublic <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, @Nullable Object... uriVariables) throws RestClientException Description copied from interface:RestOperationsExecute the HTTP method to the given URI template, writing the given request entity to the request, and return the response asResponseEntity.URI Template variables are expanded using the given URI variables, if any. - Specified by:
- exchangein interface- RestOperations
- Parameters:
- url- the URL
- method- 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.classfor no body
- uriVariables- the variables to expand in the template
- Returns:
- the response as entity
- Throws:
- RestClientException
 
- 
exchangepublic <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ? extends @Nullable Object> uriVariables) throws RestClientExceptionDescription copied from interface:RestOperationsExecute the HTTP method to the given URI template, writing the given request entity to the request, and return the response asResponseEntity.URI Template variables are expanded using the given URI variables, if any. - Specified by:
- exchangein interface- RestOperations
- Parameters:
- url- the URL
- method- 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.classfor no body
- uriVariables- the variables to expand in the template
- Returns:
- the response as entity
- Throws:
- RestClientException
 
- 
exchangepublic <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType) throws RestClientException Description copied from interface:RestOperationsExecute the HTTP method to the given URI template, writing the given request entity to the request, and return the response asResponseEntity.- Specified by:
- exchangein interface- RestOperations
- Parameters:
- url- the URL
- method- 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.classfor no body
- Returns:
- the response as entity
- Throws:
- RestClientException
 
- 
exchangepublic <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, @Nullable Object... uriVariables) throws RestClientException Description copied from interface:RestOperationsExecute the HTTP method to the given URI template, writing the given request entity to the request, and return the response asResponseEntity. The givenParameterizedTypeReferenceis used to pass generic type information:ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {}; ResponseEntity<List<MyBean>> response = template.exchange("https://example.com",HttpMethod.GET, null, myBean);- Specified by:
- exchangein interface- RestOperations
- Parameters:
- url- the URL
- method- 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.classfor no body
- uriVariables- the variables to expand in the template
- Returns:
- the response as entity
- Throws:
- RestClientException
 
- 
exchangepublic <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String, ? extends @Nullable Object> uriVariables) throws RestClientExceptionDescription copied from interface:RestOperationsExecute the HTTP method to the given URI template, writing the given request entity to the request, and return the response asResponseEntity. The givenParameterizedTypeReferenceis used to pass generic type information:ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {}; ResponseEntity<List<MyBean>> response = template.exchange("https://example.com",HttpMethod.GET, null, myBean);- Specified by:
- exchangein interface- RestOperations
- Parameters:
- url- the URL
- method- 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.classfor no body
- uriVariables- the variables to expand in the template
- Returns:
- the response as entity
- Throws:
- RestClientException
 
- 
exchangepublic <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException Description copied from interface:RestOperationsExecute the HTTP method to the given URI template, writing the given request entity to the request, and return the response asResponseEntity. The givenParameterizedTypeReferenceis used to pass generic type information:ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {}; ResponseEntity<List<MyBean>> response = template.exchange("https://example.com",HttpMethod.GET, null, myBean);- Specified by:
- exchangein interface- RestOperations
- Parameters:
- url- the URL
- method- 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.classfor no body
- Returns:
- the response as entity
- Throws:
- RestClientException
 
- 
exchangepublic <T> ResponseEntity<T> exchange(RequestEntity<?> entity, Class<T> responseType) throws RestClientException Description copied from interface:RestOperationsExecute the request specified in the givenRequestEntityand return the response asResponseEntity. Typically used in combination with the static builder methods onRequestEntity, for instance:MyRequest body = ... RequestEntity request = RequestEntity .post(URI.create("https://example.com/foo")) .accept(MediaType.APPLICATION_JSON) .body(body); ResponseEntity<MyResponse> response = template.exchange(request, MyResponse.class);- Specified by:
- exchangein interface- RestOperations
- Parameters:
- entity- the entity to write to the request
- responseType- the type to convert the response to, or- Void.classfor no body
- Returns:
- the response as entity
- Throws:
- RestClientException
 
- 
exchangepublic <T> ResponseEntity<T> exchange(RequestEntity<?> entity, ParameterizedTypeReference<T> responseType) throws RestClientException Description copied from interface:RestOperationsExecute the request specified in the givenRequestEntityand return the response asResponseEntity. The givenParameterizedTypeReferenceis used to pass generic type information:MyRequest body = ... RequestEntity request = RequestEntity .post(URI.create("https://example.com/foo")) .accept(MediaType.APPLICATION_JSON) .body(body); ParameterizedTypeReference<List<MyResponse>> myBean = new ParameterizedTypeReference<List<MyResponse>>() {}; ResponseEntity<List<MyResponse>> response = template.exchange(request, myBean);- Specified by:
- exchangein interface- RestOperations
- Parameters:
- entity- the entity to write to the request
- responseType- the type to convert the response to, or- Void.classfor no body
- Returns:
- the response as entity
- Throws:
- RestClientException
 
- 
executepublic <T> @Nullable T execute(String uriTemplate, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, @Nullable Object... uriVariables) throws RestClientException Execute the HTTP method to the given URI template, preparing the request with theRequestCallback, and reading the response with aResponseExtractor.URI Template variables are expanded using the given URI variables, if any. To provide a RequestCallbackorResponseExtractoronly, but not both, consider using:- Specified by:
- executein interface- RestOperations
- Parameters:
- uriTemplate- the URI template
- method- the HTTP method (GET, POST, etc)
- requestCallback- object that prepares the request
- responseExtractor- object that extracts the return value from the response
- uriVariables- the variables to expand in the template
- Returns:
- an arbitrary object, as returned by the ResponseExtractor
- Throws:
- RestClientException
 
- 
executepublic <T> @Nullable T execute(String uriTemplate, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, Map<String, ? extends @Nullable Object> uriVariables) throws RestClientExceptionExecute the HTTP method to the given URI template, preparing the request with theRequestCallback, and reading the response with aResponseExtractor.URI Template variables are expanded using the given URI variables map. To provide a RequestCallbackorResponseExtractoronly, but not both, consider using:- Specified by:
- executein interface- RestOperations
- Parameters:
- uriTemplate- the URI template
- method- the HTTP method (GET, POST, etc)
- requestCallback- object that prepares the request
- responseExtractor- object that extracts the return value from the response
- uriVariables- the variables to expand in the template
- Returns:
- an arbitrary object, as returned by the ResponseExtractor
- Throws:
- RestClientException
 
- 
executepublic <T> @Nullable T execute(URI url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) throws RestClientException Execute the HTTP method to the given URL, preparing the request with theRequestCallback, and reading the response with aResponseExtractor.To provide a RequestCallbackorResponseExtractoronly, but not both, consider using:- Specified by:
- executein interface- RestOperations
- Parameters:
- url- the URL
- method- the HTTP method (GET, POST, etc)
- requestCallback- object that prepares the request
- responseExtractor- object that extracts the return value from the response
- Returns:
- an arbitrary object, as returned by the ResponseExtractor
- Throws:
- RestClientException
 
- 
doExecuteprotected <T> @Nullable T doExecute(URI url, @Nullable String uriTemplate, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) throws RestClientException Execute the given method on the provided URI.The ClientHttpRequestis processed using theRequestCallback; the response with theResponseExtractor.- Parameters:
- url- the fully-expanded URL to connect to
- uriTemplate- the URI template that was used for creating the expanded URL
- method- 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)
- Returns:
- an arbitrary object, as returned by the ResponseExtractor
- Throws:
- RestClientException
- Since:
- 6.0
 
- 
handleResponseprotected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException Handle the given response, performing appropriate logging and invoking theResponseErrorHandlerif necessary.Can be overridden in subclasses. - Parameters:
- url- the fully-expanded URL to connect to
- method- the HTTP method to execute (GET, POST, etc.)
- response- the resulting- ClientHttpResponse
- Throws:
- IOException- if propagated from- ResponseErrorHandler
- Since:
- 4.1.6
- See Also:
 
- 
acceptHeaderRequestCallbackReturn aRequestCallbackthat sets the requestAcceptheader based on the given response type, cross-checked against the configured message converters.
- 
httpEntityCallbackReturn aRequestCallbackimplementation that writes the given object to the request stream.
- 
httpEntityCallbackReturn aRequestCallbackimplementation that:- Sets the request Acceptheader based on the given response type, cross-checked against the configured message converters.
- Writes the given object to the request stream.
 
- Sets the request 
- 
responseEntityExtractorReturn aResponseExtractorthat prepares aResponseEntity.
- 
headersExtractorReturn a response extractor forHttpHeaders.
 
-