Top Description Inners Fields Constructors Methods
org.springframework.web.client

public Class RestTemplate

extends InterceptingHttpAccessor
implements RestOperations
Class Inheritance
All Implemented Interfaces
org.springframework.web.client.RestOperations
Imports
java.io.IOException, java.lang.reflect.ParameterizedType, .Type, java.net.URI, java.util.ArrayList, .Collections, .List, .Map, .Set, java.util.stream.Collectors, .Stream, io.micrometer.observation.Observation, .ObservationConvention, .ObservationRegistry, org.springframework.core.ParameterizedTypeReference, org.springframework.http.HttpEntity, .HttpHeaders, .HttpMethod, .HttpStatusCode, .MediaType, .RequestEntity, .ResponseEntity, org.springframework.http.client.ClientHttpRequest, .ClientHttpRequestFactory, .ClientHttpResponse, org.springframework.http.client.observation.ClientHttpObservationDocumentation, .ClientRequestObservationContext, .ClientRequestObservationConvention, .DefaultClientRequestObservationConvention, org.springframework.http.client.support.InterceptingHttpAccessor, org.springframework.http.converter.ByteArrayHttpMessageConverter, .GenericHttpMessageConverter, .HttpMessageConverter, .ResourceHttpMessageConverter, .StringHttpMessageConverter, org.springframework.http.converter.cbor.KotlinSerializationCborHttpMessageConverter, .MappingJackson2CborHttpMessageConverter, org.springframework.http.converter.feed.AtomFeedHttpMessageConverter, .RssChannelHttpMessageConverter, org.springframework.http.converter.json.GsonHttpMessageConverter, .JsonbHttpMessageConverter, .KotlinSerializationJsonHttpMessageConverter, .MappingJackson2HttpMessageConverter, org.springframework.http.converter.protobuf.KotlinSerializationProtobufHttpMessageConverter, org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageConverter, org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter, org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter, .MappingJackson2XmlHttpMessageConverter, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .MimeTypeUtils, org.springframework.web.util.DefaultUriBuilderFactory, .DefaultUriBuilderFactory.EncodingMode, .UriTemplateHandler

Synchronous client to perform HTTP requests, exposing a simple, template method API over underlying HTTP client libraries such as the JDK 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.springframework.web.reactive.function.client.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.

Authors
Arjen Poutsma, Brian Clozel, Roy Clarkson, Juergen Hoeller, Sam Brannen, Sebastien Deleuze
Since
3.0
See Also
HttpMessageConverter, RequestCallback, ResponseExtractor, ResponseErrorHandler

Nested and Inner Type Summary

Modifier and TypeClass 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 HttpHeaders.

private class
RestTemplate.HttpEntityRequestCallback

Request callback implementation that writes the given object to the request stream.

private class

Field Summary

Modifier and TypeField and Description
private static final ClientRequestObservationConvention
private ResponseErrorHandler
private static final boolean
private final ResponseExtractor<HttpHeaders>
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<HttpMessageConverter<?>>
private ClientRequestObservationConvention
private ObservationRegistry
private static final boolean
private UriTemplateHandler

Constructor Summary

AccessConstructor and Description
public
RestTemplate()

Create a new instance of the RestTemplate using default settings.

public
RestTemplate(ClientHttpRequestFactory
the HTTP request factory to use
requestFactory
)

Create a new instance of the RestTemplate based on the given ClientHttpRequestFactory.

public
RestTemplate(List<HttpMessageConverter<?>>
the list of HttpMessageConverter to use
messageConverters
)

Create a new instance of the RestTemplate using the given list of HttpMessageConverter to use.

Method Summary

Modifier and TypeMethod and Description
public <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.

private static ResourceAccessException
public void
delete(String
the URL
url
,
Object...
the variables to expand in the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.delete.

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.springframework.web.client.RestOperations.delete.

Delete the resources at the specified URI.
public void
delete(URI
the URL
url
)

Implements org.springframework.web.client.RestOperations.delete.

Delete the resources at the specified URL.
protected <T> T

Returns:

an arbitrary object, as returned by the ResponseExtractor
doExecute
(URI
the 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 null)
requestCallback
,
ResponseExtractor<T>
object that extracts the return value from the response (can be null)
responseExtractor
)

Execute the given method on the provided URI.
protected <T> T

Returns:

an arbitrary object, as returned by the ResponseExtractor
doExecute
(URI
the 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 null)
requestCallback
,
ResponseExtractor<T>
object that extracts the return value from the response (can be null)
responseExtractor
)

Execute the given method on the provided URI.

public <T> ResponseEntity<T>
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 null)
requestEntity
,
Class<T>
the type to convert the response to, or Void.class for no body
responseType
,
Object...
the variables to expand in the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.exchange.

Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response as ResponseEntity.
public <T> ResponseEntity<T>
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 null)
requestEntity
,
Class<T>
the type to convert the response to, or Void.class for no body
responseType
,
Map<String, ?>
the variables to expand in the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.exchange.

Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response as ResponseEntity.
public <T> ResponseEntity<T>
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 null)
requestEntity
,
Class<T>
the type to convert the response to, or Void.class for no body
responseType
)

Implements org.springframework.web.client.RestOperations.exchange.

Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response as ResponseEntity.
public <T> ResponseEntity<T>
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 null)
requestEntity
,
ParameterizedTypeReference<T>
the type to convert the response to, or Void.class for no body
responseType
,
Object...
the variables to expand in the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.exchange.

Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response as ResponseEntity.
public <T> ResponseEntity<T>
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 null)
requestEntity
,
ParameterizedTypeReference<T>
the type to convert the response to, or Void.class for no body
responseType
,
Map<String, ?>
the variables to expand in the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.exchange.

Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response as ResponseEntity.
public <T> ResponseEntity<T>
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 null)
requestEntity
,
ParameterizedTypeReference<T>
the type to convert the response to, or Void.class for no body
responseType
)

Implements org.springframework.web.client.RestOperations.exchange.

Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response as ResponseEntity.
public <T> ResponseEntity<T>
exchange(RequestEntity<?>
the entity to write to the request
entity
,
Class<T>
the type to convert the response to, or Void.class for no body
responseType
)

Implements org.springframework.web.client.RestOperations.exchange.

Execute the request specified in the given RequestEntity and return the response as ResponseEntity.
public <T> ResponseEntity<T>
exchange(RequestEntity<?>
the entity to write to the request
entity
,
ParameterizedTypeReference<T>
the type to convert the response to, or Void.class for no body
responseType
)

Implements org.springframework.web.client.RestOperations.exchange.

Execute the request specified in the given 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.springframework.web.client.RestOperations.execute.

Execute the HTTP method to the given URI template, preparing the request with the 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.springframework.web.client.RestOperations.execute.

Execute the HTTP method to the given URI template, preparing the request with the 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.springframework.web.client.RestOperations.execute.

Execute the HTTP method to the given URL, preparing the request with the RequestCallback, and reading the response with a ResponseExtractor.
public ResponseErrorHandler
getErrorHandler()

Return the error handler.

public <T> ResponseEntity<T>
getForEntity(String
the URL
url
,
Class<T>
the type of the return value
responseType
,
Object...
the variables to expand the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.getForEntity.

Retrieve an entity by doing a GET on the specified URL.
public <T> ResponseEntity<T>
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.springframework.web.client.RestOperations.getForEntity.

Retrieve a representation by doing a GET on the URI template.
public <T> ResponseEntity<T>
getForEntity(URI
the URL
url
,
Class<T>
the type of the return value
responseType
)

Implements org.springframework.web.client.RestOperations.getForEntity.

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.springframework.web.client.RestOperations.getForObject.

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.springframework.web.client.RestOperations.getForObject.

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.springframework.web.client.RestOperations.getForObject.

Retrieve a representation by doing a GET on the URL.
public List<HttpMessageConverter<?>>
getMessageConverters()

Return the list of message body converters.

public ClientRequestObservationConvention
getObservationConvention()

Return the configured ClientRequestObservationConvention, or null if not set.

public ObservationRegistry
public UriTemplateHandler
getUriTemplateHandler()

Return the configured URI template handler.

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 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.

public HttpHeaders
headForHeaders(String
the URL
url
,
Object...
the variables to expand the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.headForHeaders.

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.springframework.web.client.RestOperations.headForHeaders.

Retrieve all headers of the resource specified by the URI template.
public HttpHeaders
headForHeaders(URI
the URL
url
)

Implements org.springframework.web.client.RestOperations.headForHeaders.

Retrieve all headers of the resource specified by the URL.
public <T> RequestCallback
httpEntityCallback(Object requestBody)

Return a RequestCallback implementation that writes the given object to the request stream.

public <T> RequestCallback
httpEntityCallback(Object requestBody, Type responseType)

Return a RequestCallback implementation that:

  1. Sets the request Accept header based on the given response type, cross-checked against the configured message converters.
  2. Writes the given object to the request stream.
private static DefaultUriBuilderFactory
private static <T> T
nonNull(T result)

public Set<HttpMethod>
optionsForAllow(String
the URL
url
,
Object...
the variables to expand in the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.optionsForAllow.

Return the value of the Allow header for the given URI.
public Set<HttpMethod>
optionsForAllow(String
the URL
url
,
Map<String, ?>
the variables to expand in the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.optionsForAllow.

Return the value of the Allow header for the given URI.
public Set<HttpMethod>
optionsForAllow(URI
the URL
url
)

Implements org.springframework.web.client.RestOperations.optionsForAllow.

Return the value of the Allow header for the given URL.
public <T> T
patchForObject(String
the URL
url
,
Object
the object to be PATCHed (may be null)
request
,
Class<T>
the type of the return value
responseType
,
Object...
the variables to expand the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.patchForObject.

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 null)
request
,
Class<T>
the type of the return value
responseType
,
Map<String, ?>
the variables to expand the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.patchForObject.

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 null)
request
,
Class<T>
the type of the return value
responseType
)

Implements org.springframework.web.client.RestOperations.patchForObject.

Update a resource by PATCHing the given object to the URL, and return the representation found in the response.
public <T> ResponseEntity<T>
postForEntity(String
the URL
url
,
Object
the Object to be POSTed (may be null)
request
,
Class<T> responseType, Object...
the variables to expand the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.postForEntity.

Create a new resource by POSTing the given object to the URI template, and return the response as ResponseEntity.
public <T> ResponseEntity<T>
postForEntity(String
the URL
url
,
Object
the Object to be POSTed (may be null)
request
,
Class<T> responseType, Map<String, ?>
the variables to expand the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.postForEntity.

Create a new resource by POSTing the given object to the URI template, and return the response as HttpEntity.
public <T> ResponseEntity<T>
postForEntity(URI
the URL
url
,
Object
the Object to be POSTed (may be null)
request
,
Class<T> responseType)

Implements org.springframework.web.client.RestOperations.postForEntity.

Create a new resource by POSTing the given object to the URL, and return the response as ResponseEntity.
public URI
postForLocation(String
the URL
url
,
Object
the Object to be POSTed (may be null)
request
,
Object...
the variables to expand the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.postForLocation.

Create a new resource by POSTing the given object to the URI template, and return the value of the Location header.
public URI
postForLocation(String
the URL
url
,
Object
the Object to be POSTed (may be null)
request
,
Map<String, ?>
the variables to expand the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.postForLocation.

Create a new resource by POSTing the given object to the URI template, and return the value of the Location header.
public URI
postForLocation(URI
the URL
url
,
Object
the Object to be POSTed (may be null)
request
)

Implements org.springframework.web.client.RestOperations.postForLocation.

Create a new resource by POSTing the given object to the URL, and return the value of the Location header.
public <T> T
postForObject(String
the URL
url
,
Object
the Object to be POSTed (may be null)
request
,
Class<T>
the type of the return value
responseType
,
Object...
the variables to expand the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.postForObject.

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 null)
request
,
Class<T>
the type of the return value
responseType
,
Map<String, ?>
the variables to expand the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.postForObject.

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 null)
request
,
Class<T>
the type of the return value
responseType
)

Implements org.springframework.web.client.RestOperations.postForObject.

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 null)
request
,
Object...
the variables to expand the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.put.

Create or update a resource by PUTting the given object to the URI.
public void
put(String
the URL
url
,
Object
the Object to be PUT (may be null)
request
,
Map<String, ?>
the variables to expand the template
uriVariables
)

Implements org.springframework.web.client.RestOperations.put.

Creates a new resource by PUTting the given object to URI template.
public void
put(URI
the URL
url
,
Object
the Object to be PUT (may be null)
request
)

Implements org.springframework.web.client.RestOperations.put.

Creates a new resource by PUTting the given object to URL.
private String
private URI
public <T> ResponseExtractor<ResponseEntity<T>>
responseEntityExtractor(Type responseType)

Return a ResponseExtractor that prepares a ResponseEntity.

public void
setDefaultUriVariables(Map<String, ?>
the default URI variable values
uriVars
)

Configure default URI variable values.

public void
setErrorHandler(ResponseErrorHandler errorHandler)

Set the error handler.

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 ObservationConvention that sets the name of the observation as well as its io.micrometer.common.KeyValues extracted from the ClientRequestObservationContext.

public void
setObservationRegistry(ObservationRegistry
the observation registry to use
observationRegistry
)

Configure an ObservationRegistry for collecting spans and metrics for request execution.

public void
setUriTemplateHandler(UriTemplateHandler
the URI template handler to use
handler
)

Configure a strategy for expanding URI templates.

private void
private void
Inherited from org.springframework.http.client.support.InterceptingHttpAccessor:
getInterceptorsgetRequestFactorysetInterceptorssetRequestFactory