类 RestTemplate
- java.lang.Object
- org.springframework.http.client.support.HttpAccessor
- org.springframework.http.client.support.InterceptingHttpAccessor
- org.springframework.web.client.RestTemplate
- 所有已实现的接口:
RestOperations
public class RestTemplate extends InterceptingHttpAccessor implements RestOperations
Synchronous client to perform HTTP requests, exposing a simple, template method API over underlying HTTP client libraries such as the JDKHttpURLConnection
, Apache HttpComponents, and others.The RestTemplate offers templates for common scenarios by HTTP method, in addition to the generalized
exchange
andexecute
methods that support of less frequent cases.NOTE: As of 5.0 this class is in maintenance mode, with only minor requests for changes and bugs to be accepted going forward. Please, consider using the
org.springframework.web.reactive.client.WebClient
which has a more modern API and supports sync, async, and streaming scenarios.- 从以下版本开始:
- 3.0
- 作者:
- Arjen Poutsma, Brian Clozel, Roy Clarkson, Juergen Hoeller, Sam Brannen
- 另请参阅:
HttpMessageConverter
,RequestCallback
,ResponseExtractor
,ResponseErrorHandler
字段概要
从类继承的字段 org.springframework.http.client.support.HttpAccessor
logger
构造器概要
构造器 构造器 说明 RestTemplate()
Create a new instance of theRestTemplate
using default settings.RestTemplate(List<HttpMessageConverter<?>> messageConverters)
Create a new instance of theRestTemplate
using the given list ofHttpMessageConverter
to use.RestTemplate(ClientHttpRequestFactory requestFactory)
Create a new instance of theRestTemplate
based on the givenClientHttpRequestFactory
.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 <T> RequestCallback
acceptHeaderRequestCallback(Class<T> responseType)
Return aRequestCallback
that sets the requestAccept
header based on the given response type, cross-checked against the configured message converters.void
delete(String url, Object... uriVariables)
Delete the resources at the specified URI.void
delete(String url, Map<String,?> uriVariables)
Delete the resources at the specified URI.void
delete(URI url)
Delete the resources at the specified URL.protected <T> T
doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor)
Execute the given method on the provided URI.<T> ResponseEntity<T>
exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables)
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
.<T> ResponseEntity<T>
exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Map<String,?> uriVariables)
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
.<T> ResponseEntity<T>
exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Object... uriVariables)
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
.<T> ResponseEntity<T>
exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String,?> uriVariables)
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
.<T> ResponseEntity<T>
exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType)
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
.<T> ResponseEntity<T>
exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType)
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
.<T> ResponseEntity<T>
exchange(RequestEntity<?> requestEntity, Class<T> responseType)
Execute the request specified in the givenRequestEntity
and return the response asResponseEntity
.<T> ResponseEntity<T>
exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType)
Execute the request specified in the givenRequestEntity
and return the response asResponseEntity
.<T> T
execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Object... uriVariables)
Execute the HTTP method to the given URI template, preparing the request with theRequestCallback
, and reading the response with aResponseExtractor
.<T> T
execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Map<String,?> uriVariables)
Execute the HTTP method to the given URI template, preparing the request with theRequestCallback
, and reading the response with aResponseExtractor
.<T> T
execute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor)
Execute the HTTP method to the given URL, preparing the request with theRequestCallback
, and reading the response with aResponseExtractor
.ResponseErrorHandler
getErrorHandler()
Return the error handler.<T> ResponseEntity<T>
getForEntity(String url, Class<T> responseType, Object... uriVariables)
Retrieve an entity by doing a GET on the specified URL.<T> ResponseEntity<T>
getForEntity(String url, Class<T> responseType, Map<String,?> 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> T
getForObject(String url, Class<T> responseType, Object... uriVariables)
Retrieve a representation by doing a GET on the specified URL.<T> T
getForObject(String url, Class<T> responseType, Map<String,?> uriVariables)
Retrieve a representation by doing a GET on the URI template.<T> T
getForObject(URI url, Class<T> responseType)
Retrieve a representation by doing a GET on the URL .List<HttpMessageConverter<?>>
getMessageConverters()
Return the list of message body converters.UriTemplateHandler
getUriTemplateHandler()
Return the configured URI template handler.protected void
handleResponse(URI url, HttpMethod method, ClientHttpResponse response)
Handle the given response, performing appropriate logging and invoking theResponseErrorHandler
if necessary.protected ResponseExtractor<HttpHeaders>
headersExtractor()
Return a response extractor forHttpHeaders
.HttpHeaders
headForHeaders(String url, Object... uriVariables)
Retrieve all headers of the resource specified by the URI template.HttpHeaders
headForHeaders(String url, Map<String,?> uriVariables)
Retrieve all headers of the resource specified by the URI template.HttpHeaders
headForHeaders(URI url)
Retrieve all headers of the resource specified by the URL.<T> RequestCallback
httpEntityCallback(Object requestBody)
Return aRequestCallback
implementation that writes the given object to the request stream.<T> RequestCallback
httpEntityCallback(Object requestBody, Type responseType)
Return aRequestCallback
implementation that: Sets the requestAccept
header based on the given response type, cross-checked against the configured message converters.Set<HttpMethod>
optionsForAllow(String url, Object... uriVariables)
Return the value of the Allow header for the given URI.Set<HttpMethod>
optionsForAllow(String url, Map<String,?> uriVariables)
Return the value of the Allow header for the given URI.Set<HttpMethod>
optionsForAllow(URI url)
Return the value of the Allow header for the given URL.<T> T
patchForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
Update a resource by PATCHing the given object to the URI template, and return the representation found in the response.<T> T
patchForObject(String url, Object request, Class<T> responseType, Map<String,?> uriVariables)
Update a resource by PATCHing the given object to the URI template, and return the representation found in the response.<T> T
patchForObject(URI url, Object request, Class<T> responseType)
Update a resource by PATCHing the given object to the URL, and return the representation found in the response.<T> ResponseEntity<T>
postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables)
Create a new resource by POSTing the given object to the URI template, and returns the response asResponseEntity
.<T> ResponseEntity<T>
postForEntity(String url, Object request, Class<T> responseType, Map<String,?> uriVariables)
Create a new resource by POSTing the given object to the URI template, and returns the response asHttpEntity
.<T> ResponseEntity<T>
postForEntity(URI url, Object request, Class<T> responseType)
Create a new resource by POSTing the given object to the URL, and returns the response asResponseEntity
.URI
postForLocation(String url, Object request, Object... uriVariables)
Create a new resource by POSTing the given object to the URI template, and returns the value of theLocation
header.URI
postForLocation(String url, Object request, Map<String,?> uriVariables)
Create a new resource by POSTing the given object to the URI template, and returns the value of theLocation
header.URI
postForLocation(URI url, Object request)
Create a new resource by POSTing the given object to the URL, and returns the value of theLocation
header.<T> T
postForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
Create a new resource by POSTing the given object to the URI template, and returns the representation found in the response.<T> T
postForObject(String url, Object request, Class<T> responseType, Map<String,?> uriVariables)
Create a new resource by POSTing the given object to the URI template, and returns the representation found in the response.<T> T
postForObject(URI url, Object request, Class<T> responseType)
Create a new resource by POSTing the given object to the URL, and returns the representation found in the response.void
put(String url, Object request, Object... uriVariables)
Create or update a resource by PUTting the given object to the URI.void
put(String url, Object request, Map<String,?> uriVariables)
Creates a new resource by PUTting the given object to URI template.void
put(URI url, Object request)
Creates a new resource by PUTting the given object to URL.<T> ResponseExtractor<ResponseEntity<T>>
responseEntityExtractor(Type responseType)
Return aResponseExtractor
that prepares aResponseEntity
.void
setDefaultUriVariables(Map<String,?> uriVars)
Configure default URI variable values.void
setErrorHandler(ResponseErrorHandler errorHandler)
Set the error handler.void
setMessageConverters(List<HttpMessageConverter<?>> messageConverters)
Set the message body converters to use.void
setUriTemplateHandler(UriTemplateHandler handler)
Configure a strategy for expanding URI templates.从类继承的方法 org.springframework.http.client.support.InterceptingHttpAccessor
getInterceptors, getRequestFactory, setInterceptors, setRequestFactory
从类继承的方法 org.springframework.http.client.support.HttpAccessor
createRequest, getClientHttpRequestInitializers, setClientHttpRequestInitializers
构造器详细资料
RestTemplate
public RestTemplate()
Create a new instance of theRestTemplate
using default settings. DefaultHttpMessageConverters
are initialized.
RestTemplate
public RestTemplate(ClientHttpRequestFactory requestFactory)
Create a new instance of theRestTemplate
based on the givenClientHttpRequestFactory
.- 参数:
requestFactory
- the HTTP request factory to use- 另请参阅:
SimpleClientHttpRequestFactory
,HttpComponentsClientHttpRequestFactory
RestTemplate
public RestTemplate(List<HttpMessageConverter<?>> messageConverters)
Create a new instance of theRestTemplate
using the given list ofHttpMessageConverter
to use.- 参数:
messageConverters
- the list ofHttpMessageConverter
to use- 从以下版本开始:
- 3.2.7
方法详细资料
setMessageConverters
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters)
Set the message body converters to use.These converters are used to convert from and to HTTP requests and responses.
getMessageConverters
public List<HttpMessageConverter<?>> getMessageConverters()
Return the list of message body converters.The returned
List
is active and may get appended to.
setErrorHandler
public void setErrorHandler(ResponseErrorHandler errorHandler)
Set the error handler.By default, RestTemplate uses a
DefaultResponseErrorHandler
.
getErrorHandler
public ResponseErrorHandler getErrorHandler()
Return the error handler.
setDefaultUriVariables
public void setDefaultUriVariables(Map<String,?> uriVars)
Configure default URI variable values. This is a shortcut for:DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(); handler.setDefaultUriVariables(...); RestTemplate restTemplate = new RestTemplate(); restTemplate.setUriTemplateHandler(handler);
- 参数:
uriVars
- the default URI variable values- 从以下版本开始:
- 4.3
setUriTemplateHandler
public void setUriTemplateHandler(UriTemplateHandler handler)
Configure a strategy for expanding URI templates.By default,
DefaultUriBuilderFactory
is used and for backwards compatibility, the encoding mode is set toURI_COMPONENT
. As of 5.0.8, prefer usingTEMPLATE_AND_VALUES
.Note: in 5.0 the switch from
DefaultUriTemplateHandler
(deprecated in 4.3), as the default to use, toDefaultUriBuilderFactory
brings in a different default for theparsePath
property (switching from false to true).- 参数:
handler
- the URI template handler to use
getUriTemplateHandler
public UriTemplateHandler getUriTemplateHandler()
Return the configured URI template handler.
getForObject
@Nullable public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Retrieve 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.
- 指定者:
getForObject
在接口中RestOperations
- 参数:
url
- the URLresponseType
- the type of the return valueuriVariables
- the variables to expand the template- 返回:
- the converted object
- 抛出:
RestClientException
getForObject
@Nullable public <T> T getForObject(String url, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Retrieve 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.
- 指定者:
getForObject
在接口中RestOperations
- 参数:
url
- the URLresponseType
- the type of the return valueuriVariables
- the map containing variables for the URI template- 返回:
- the converted object
- 抛出:
RestClientException
getForObject
@Nullable public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException
从接口复制的说明:RestOperations
Retrieve a representation by doing a GET on the URL . The response (if any) is converted and returned.- 指定者:
getForObject
在接口中RestOperations
- 参数:
url
- the URLresponseType
- the type of the return value- 返回:
- the converted object
- 抛出:
RestClientException
getForEntity
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Retrieve an entity by doing a GET on the specified URL. The response is converted and stored in anResponseEntity
.URI Template variables are expanded using the given URI variables, if any.
- 指定者:
getForEntity
在接口中RestOperations
- 参数:
url
- the URLresponseType
- the type of the return valueuriVariables
- the variables to expand the template- 返回:
- the entity
- 抛出:
RestClientException
getForEntity
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Retrieve a representation by doing a GET on the URI template. The response is converted and stored in anResponseEntity
.URI Template variables are expanded using the given map.
- 指定者:
getForEntity
在接口中RestOperations
- 参数:
url
- the URLresponseType
- the type of the return valueuriVariables
- the map containing variables for the URI template- 返回:
- the converted object
- 抛出:
RestClientException
getForEntity
public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException
从接口复制的说明:RestOperations
Retrieve a representation by doing a GET on the URL . The response is converted and stored in anResponseEntity
.- 指定者:
getForEntity
在接口中RestOperations
- 参数:
url
- the URLresponseType
- the type of the return value- 返回:
- the converted object
- 抛出:
RestClientException
headForHeaders
public HttpHeaders headForHeaders(String url, Object... uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Retrieve all headers of the resource specified by the URI template.URI Template variables are expanded using the given URI variables, if any.
- 指定者:
headForHeaders
在接口中RestOperations
- 参数:
url
- the URLuriVariables
- the variables to expand the template- 返回:
- all HTTP headers of that resource
- 抛出:
RestClientException
headForHeaders
public HttpHeaders headForHeaders(String url, Map<String,?> uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Retrieve all headers of the resource specified by the URI template.URI Template variables are expanded using the given map.
- 指定者:
headForHeaders
在接口中RestOperations
- 参数:
url
- the URLuriVariables
- the map containing variables for the URI template- 返回:
- all HTTP headers of that resource
- 抛出:
RestClientException
headForHeaders
public HttpHeaders headForHeaders(URI url) throws RestClientException
从接口复制的说明:RestOperations
Retrieve all headers of the resource specified by the URL.- 指定者:
headForHeaders
在接口中RestOperations
- 参数:
url
- the URL- 返回:
- all HTTP headers of that resource
- 抛出:
RestClientException
postForLocation
@Nullable public URI postForLocation(String url, @Nullable Object request, Object... uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Create a new resource by POSTing the given object to the URI template, and returns the value of theLocation
header. This header typically indicates where the new resource is stored.URI Template variables are expanded using the given URI variables, if any.
The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.The body of the entity, or
request
itself, can be aMultiValueMap
to create a multipart request. The values in theMultiValueMap
can be any Object representing the body of the part, or anHttpEntity
representing a part with body and headers.- 指定者:
postForLocation
在接口中RestOperations
- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)uriVariables
- the variables to expand the template- 返回:
- the value for the
Location
header - 抛出:
RestClientException
- 另请参阅:
HttpEntity
postForLocation
@Nullable public URI postForLocation(String url, @Nullable Object request, Map<String,?> uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Create a new resource by POSTing the given object to the URI template, and returns the value of theLocation
header. This header typically indicates where the new resource is stored.URI Template variables are expanded using the given map.
The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the requestThe body of the entity, or
request
itself, can be aMultiValueMap
to create a multipart request. The values in theMultiValueMap
can be any Object representing the body of the part, or anHttpEntity
representing a part with body and headers.- 指定者:
postForLocation
在接口中RestOperations
- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)uriVariables
- the variables to expand the template- 返回:
- the value for the
Location
header - 抛出:
RestClientException
- 另请参阅:
HttpEntity
postForLocation
@Nullable public URI postForLocation(URI url, @Nullable Object request) throws RestClientException
从接口复制的说明:RestOperations
Create a new resource by POSTing the given object to the URL, and returns the value of theLocation
header. This header typically indicates where the new resource is stored.The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.The body of the entity, or
request
itself, can be aMultiValueMap
to create a multipart request. The values in theMultiValueMap
can be any Object representing the body of the part, or anHttpEntity
representing a part with body and headers.- 指定者:
postForLocation
在接口中RestOperations
- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)- 返回:
- the value for the
Location
header - 抛出:
RestClientException
- 另请参阅:
HttpEntity
postForObject
@Nullable public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Create a new resource by POSTing the given object to the URI template, and returns the representation found in the response.URI Template variables are expanded using the given URI variables, if any.
The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.The body of the entity, or
request
itself, can be aMultiValueMap
to create a multipart request. The values in theMultiValueMap
can be any Object representing the body of the part, or anHttpEntity
representing a part with body and headers.- 指定者:
postForObject
在接口中RestOperations
- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand the template- 返回:
- the converted object
- 抛出:
RestClientException
- 另请参阅:
HttpEntity
postForObject
@Nullable public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Create a new resource by POSTing the given object to the URI template, and returns the representation found in the response.URI Template variables are expanded using the given map.
The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.The body of the entity, or
request
itself, can be aMultiValueMap
to create a multipart request. The values in theMultiValueMap
can be any Object representing the body of the part, or anHttpEntity
representing a part with body and headers.- 指定者:
postForObject
在接口中RestOperations
- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand the template- 返回:
- the converted object
- 抛出:
RestClientException
- 另请参阅:
HttpEntity
postForObject
@Nullable public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException
从接口复制的说明:RestOperations
Create a new resource by POSTing the given object to the URL, and returns the representation found in the response.The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.The body of the entity, or
request
itself, can be aMultiValueMap
to create a multipart request. The values in theMultiValueMap
can be any Object representing the body of the part, or anHttpEntity
representing a part with body and headers.- 指定者:
postForObject
在接口中RestOperations
- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)responseType
- the type of the return value- 返回:
- the converted object
- 抛出:
RestClientException
- 另请参阅:
HttpEntity
postForEntity
public <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Create a new resource by POSTing the given object to the URI template, and returns the response asResponseEntity
.URI Template variables are expanded using the given URI variables, if any.
The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.The body of the entity, or
request
itself, can be aMultiValueMap
to create a multipart request. The values in theMultiValueMap
can be any Object representing the body of the part, or anHttpEntity
representing a part with body and headers.- 指定者:
postForEntity
在接口中RestOperations
- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)uriVariables
- the variables to expand the template- 返回:
- the converted object
- 抛出:
RestClientException
- 另请参阅:
HttpEntity
postForEntity
public <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Create a new resource by POSTing the given object to the URI template, and returns the response asHttpEntity
.URI Template variables are expanded using the given map.
The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.The body of the entity, or
request
itself, can be aMultiValueMap
to create a multipart request. The values in theMultiValueMap
can be any Object representing the body of the part, or anHttpEntity
representing a part with body and headers.- 指定者:
postForEntity
在接口中RestOperations
- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)uriVariables
- the variables to expand the template- 返回:
- the converted object
- 抛出:
RestClientException
- 另请参阅:
HttpEntity
postForEntity
public <T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException
从接口复制的说明:RestOperations
Create a new resource by POSTing the given object to the URL, and returns the response asResponseEntity
.The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.The body of the entity, or
request
itself, can be aMultiValueMap
to create a multipart request. The values in theMultiValueMap
can be any Object representing the body of the part, or anHttpEntity
representing a part with body and headers.- 指定者:
postForEntity
在接口中RestOperations
- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)- 返回:
- the converted object
- 抛出:
RestClientException
- 另请参阅:
HttpEntity
put
public void put(String url, @Nullable Object request, Object... uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Create 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
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.- 指定者:
put
在接口中RestOperations
- 参数:
url
- the URLrequest
- the Object to be PUT (may benull
)uriVariables
- the variables to expand the template- 抛出:
RestClientException
- 另请参阅:
HttpEntity
put
public void put(String url, @Nullable Object request, Map<String,?> uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Creates a new resource by PUTting the given object to URI template.URI Template variables are expanded using the given map.
The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.- 指定者:
put
在接口中RestOperations
- 参数:
url
- the URLrequest
- the Object to be PUT (may benull
)uriVariables
- the variables to expand the template- 抛出:
RestClientException
- 另请参阅:
HttpEntity
put
public void put(URI url, @Nullable Object request) throws RestClientException
从接口复制的说明:RestOperations
Creates a new resource by PUTting the given object to URL.The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.- 指定者:
put
在接口中RestOperations
- 参数:
url
- the URLrequest
- the Object to be PUT (may benull
)- 抛出:
RestClientException
- 另请参阅:
HttpEntity
patchForObject
@Nullable public <T> T patchForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Update 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
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.NOTE: The standard JDK HTTP library does not support HTTP PATCH. You need to use the Apache HttpComponents or OkHttp request factory.
- 指定者:
patchForObject
在接口中RestOperations
- 参数:
url
- the URLrequest
- the object to be PATCHed (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand the template- 返回:
- the converted object
- 抛出:
RestClientException
- 另请参阅:
HttpEntity
,InterceptingHttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
,HttpComponentsAsyncClientHttpRequestFactory
,OkHttp3ClientHttpRequestFactory
patchForObject
@Nullable public <T> T patchForObject(String url, @Nullable Object request, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Update 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
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.NOTE: The standard JDK HTTP library does not support HTTP PATCH. You need to use the Apache HttpComponents or OkHttp request factory.
- 指定者:
patchForObject
在接口中RestOperations
- 参数:
url
- the URLrequest
- the object to be PATCHed (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand the template- 返回:
- the converted object
- 抛出:
RestClientException
- 另请参阅:
HttpEntity
,InterceptingHttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
,HttpComponentsAsyncClientHttpRequestFactory
,OkHttp3ClientHttpRequestFactory
patchForObject
@Nullable public <T> T patchForObject(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException
从接口复制的说明:RestOperations
Update a resource by PATCHing the given object to the URL, and return the representation found in the response.The
request
parameter can be aHttpEntity
in order to add additional HTTP headers to the request.NOTE: The standard JDK HTTP library does not support HTTP PATCH. You need to use the Apache HttpComponents or OkHttp request factory.
- 指定者:
patchForObject
在接口中RestOperations
- 参数:
url
- the URLrequest
- the object to be PATCHed (may benull
)responseType
- the type of the return value- 返回:
- the converted object
- 抛出:
RestClientException
- 另请参阅:
HttpEntity
,InterceptingHttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
,HttpComponentsAsyncClientHttpRequestFactory
,OkHttp3ClientHttpRequestFactory
delete
public void delete(String url, Object... uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Delete the resources at the specified URI.URI Template variables are expanded using the given URI variables, if any.
- 指定者:
delete
在接口中RestOperations
- 参数:
url
- the URLuriVariables
- the variables to expand in the template- 抛出:
RestClientException
delete
public void delete(String url, Map<String,?> uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Delete the resources at the specified URI.URI Template variables are expanded using the given map.
- 指定者:
delete
在接口中RestOperations
- 参数:
url
- the URLuriVariables
- the variables to expand the template- 抛出:
RestClientException
delete
public void delete(URI url) throws RestClientException
从接口复制的说明:RestOperations
Delete the resources at the specified URL.- 指定者:
delete
在接口中RestOperations
- 参数:
url
- the URL- 抛出:
RestClientException
optionsForAllow
public Set<HttpMethod> optionsForAllow(String url, Object... uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Return the value of the Allow header for the given URI.URI Template variables are expanded using the given URI variables, if any.
- 指定者:
optionsForAllow
在接口中RestOperations
- 参数:
url
- the URLuriVariables
- the variables to expand in the template- 返回:
- the value of the allow header
- 抛出:
RestClientException
optionsForAllow
public Set<HttpMethod> optionsForAllow(String url, Map<String,?> uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Return the value of the Allow header for the given URI.URI Template variables are expanded using the given map.
- 指定者:
optionsForAllow
在接口中RestOperations
- 参数:
url
- the URLuriVariables
- the variables to expand in the template- 返回:
- the value of the allow header
- 抛出:
RestClientException
optionsForAllow
public Set<HttpMethod> optionsForAllow(URI url) throws RestClientException
从接口复制的说明:RestOperations
Return the value of the Allow header for the given URL.- 指定者:
optionsForAllow
在接口中RestOperations
- 参数:
url
- the URL- 返回:
- the value of the allow header
- 抛出:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
.URI Template variables are expanded using the given URI variables, if any.
- 指定者:
exchange
在接口中RestOperations
- 参数:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestEntity
- the entity (headers and/or body) to write to the request may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand in the template- 返回:
- the response as entity
- 抛出:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
.URI Template variables are expanded using the given URI variables, if any.
- 指定者:
exchange
在接口中RestOperations
- 参数:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestEntity
- the entity (headers and/or body) to write to the request (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand in the template- 返回:
- the response as entity
- 抛出:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType) throws RestClientException
从接口复制的说明:RestOperations
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
.- 指定者:
exchange
在接口中RestOperations
- 参数:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestEntity
- the entity (headers and/or body) to write to the request (may benull
)responseType
- the type of the return value- 返回:
- the response as entity
- 抛出:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Object... uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
. The givenParameterizedTypeReference
is used to pass generic type information:ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {}; ResponseEntity<List<MyBean>> response = template.exchange("https://example.com",HttpMethod.GET, null, myBean);
- 指定者:
exchange
在接口中RestOperations
- 参数:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestEntity
- the entity (headers and/or body) to write to the request (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand in the template- 返回:
- the response as entity
- 抛出:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String,?> uriVariables) throws RestClientException
从接口复制的说明:RestOperations
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
. The givenParameterizedTypeReference
is used to pass generic type information:ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {}; ResponseEntity<List<MyBean>> response = template.exchange("https://example.com",HttpMethod.GET, null, myBean);
- 指定者:
exchange
在接口中RestOperations
- 参数:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestEntity
- the entity (headers and/or body) to write to the request (may benull
)responseType
- the type of the return valueuriVariables
- the variables to expand in the template- 返回:
- the response as entity
- 抛出:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException
从接口复制的说明:RestOperations
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
. The givenParameterizedTypeReference
is used to pass generic type information:ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {}; ResponseEntity<List<MyBean>> response = template.exchange("https://example.com",HttpMethod.GET, null, myBean);
- 指定者:
exchange
在接口中RestOperations
- 参数:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestEntity
- the entity (headers and/or body) to write to the request (may benull
)responseType
- the type of the return value- 返回:
- the response as entity
- 抛出:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, Class<T> responseType) throws RestClientException
从接口复制的说明:RestOperations
Execute the request specified in the givenRequestEntity
and return the response asResponseEntity
. Typically used in combination with the static builder methods onRequestEntity
, for instance:MyRequest body = ... RequestEntity request = RequestEntity .post(new URI("https://example.com/foo")) .accept(MediaType.APPLICATION_JSON) .body(body); ResponseEntity<MyResponse> response = template.exchange(request, MyResponse.class);
- 指定者:
exchange
在接口中RestOperations
- 参数:
requestEntity
- the entity to write to the requestresponseType
- the type of the return value- 返回:
- the response as entity
- 抛出:
RestClientException
exchange
public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException
从接口复制的说明:RestOperations
Execute the request specified in the givenRequestEntity
and return the response asResponseEntity
. The givenParameterizedTypeReference
is used to pass generic type information:MyRequest body = ... RequestEntity request = RequestEntity .post(new URI("https://example.com/foo")) .accept(MediaType.APPLICATION_JSON) .body(body); ParameterizedTypeReference<List<MyResponse>> myBean = new ParameterizedTypeReference<List<MyResponse>>() {}; ResponseEntity<List<MyResponse>> response = template.exchange(request, myBean);
- 指定者:
exchange
在接口中RestOperations
- 参数:
requestEntity
- the entity to write to the requestresponseType
- the type of the return value- 返回:
- the response as entity
- 抛出:
RestClientException
execute
@Nullable public <T> T execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, 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
RequestCallback
orResponseExtractor
only, but not both, consider using:- 指定者:
execute
在接口中RestOperations
- 参数:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestCallback
- object that prepares the requestresponseExtractor
- object that extracts the return value from the responseuriVariables
- the variables to expand in the template- 返回:
- an arbitrary object, as returned by the
ResponseExtractor
- 抛出:
RestClientException
execute
@Nullable public <T> T execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, Map<String,?> 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 map.
To provide a
RequestCallback
orResponseExtractor
only, but not both, consider using:- 指定者:
execute
在接口中RestOperations
- 参数:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestCallback
- object that prepares the requestresponseExtractor
- object that extracts the return value from the responseuriVariables
- the variables to expand in the template- 返回:
- an arbitrary object, as returned by the
ResponseExtractor
- 抛出:
RestClientException
execute
@Nullable public <T> 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
RequestCallback
orResponseExtractor
only, but not both, consider using:- 指定者:
execute
在接口中RestOperations
- 参数:
url
- the URLmethod
- the HTTP method (GET, POST, etc)requestCallback
- object that prepares the requestresponseExtractor
- object that extracts the return value from the response- 返回:
- an arbitrary object, as returned by the
ResponseExtractor
- 抛出:
RestClientException
doExecute
@Nullable protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) throws RestClientException
Execute the given method on the provided URI.The
ClientHttpRequest
is processed using theRequestCallback
; the response with theResponseExtractor
.- 参数:
url
- the fully-expanded URL to connect tomethod
- the HTTP method to execute (GET, POST, etc.)requestCallback
- object that prepares the request (can benull
)responseExtractor
- object that extracts the return value from the response (can benull
)- 返回:
- an arbitrary object, as returned by the
ResponseExtractor
- 抛出:
RestClientException
handleResponse
protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException
Handle the given response, performing appropriate logging and invoking theResponseErrorHandler
if necessary.Can be overridden in subclasses.
- 参数:
url
- the fully-expanded URL to connect tomethod
- the HTTP method to execute (GET, POST, etc.)response
- the resultingClientHttpResponse
- 抛出:
IOException
- if propagated fromResponseErrorHandler
- 从以下版本开始:
- 4.1.6
- 另请参阅:
setErrorHandler(org.springframework.web.client.ResponseErrorHandler)
acceptHeaderRequestCallback
public <T> RequestCallback acceptHeaderRequestCallback(Class<T> responseType)
Return aRequestCallback
that sets the requestAccept
header based on the given response type, cross-checked against the configured message converters.
httpEntityCallback
public <T> RequestCallback httpEntityCallback(@Nullable Object requestBody)
Return aRequestCallback
implementation that writes the given object to the request stream.
httpEntityCallback
public <T> RequestCallback httpEntityCallback(@Nullable Object requestBody, Type responseType)
Return aRequestCallback
implementation that:- Sets the request
Accept
header based on the given response type, cross-checked against the configured message converters. - Writes the given object to the request stream.
- Sets the request
responseEntityExtractor
public <T> ResponseExtractor<ResponseEntity<T>> responseEntityExtractor(Type responseType)
Return aResponseExtractor
that prepares aResponseEntity
.
headersExtractor
protected ResponseExtractor<HttpHeaders> headersExtractor()
Return a response extractor forHttpHeaders
.