接口 RestOperations
- 所有已知实现类:
RestTemplate
public interface RestOperations
Interface specifying a basic set of RESTful operations. Implemented byRestTemplate
. Not often used directly, but a useful option to enhance testability, as it can easily be mocked or stubbed.- 从以下版本开始:
- 3.0
- 作者:
- Arjen Poutsma, Juergen Hoeller
- 另请参阅:
RestTemplate
,AsyncRestOperations
方法概要
所有方法 实例方法 抽象方法 修饰符和类型 方法 说明 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.<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
.<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 .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.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.
方法详细资料
getForObject
<T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException
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.
- 参数:
url
- the URLresponseType
- the type of the return valueuriVariables
- the variables to expand the template- 返回:
- the converted object
- 抛出:
RestClientException
getForObject
<T> T getForObject(String url, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
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.
- 参数:
url
- the URLresponseType
- the type of the return valueuriVariables
- the map containing variables for the URI template- 返回:
- the converted object
- 抛出:
RestClientException
getForObject
<T> T getForObject(URI url, Class<T> responseType) throws RestClientException
Retrieve a representation by doing a GET on the URL . The response (if any) is converted and returned.- 参数:
url
- the URLresponseType
- the type of the return value- 返回:
- the converted object
- 抛出:
RestClientException
getForEntity
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException
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.
- 参数:
url
- the URLresponseType
- the type of the return valueuriVariables
- the variables to expand the template- 返回:
- the entity
- 抛出:
RestClientException
- 从以下版本开始:
- 3.0.2
getForEntity
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
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.
- 参数:
url
- the URLresponseType
- the type of the return valueuriVariables
- the map containing variables for the URI template- 返回:
- the converted object
- 抛出:
RestClientException
- 从以下版本开始:
- 3.0.2
getForEntity
<T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException
Retrieve a representation by doing a GET on the URL . The response is converted and stored in anResponseEntity
.- 参数:
url
- the URLresponseType
- the type of the return value- 返回:
- the converted object
- 抛出:
RestClientException
- 从以下版本开始:
- 3.0.2
headForHeaders
HttpHeaders headForHeaders(String url, Object... uriVariables) throws RestClientException
Retrieve all headers of the resource specified by the URI template.URI Template variables are expanded using the given URI variables, if any.
- 参数:
url
- the URLuriVariables
- the variables to expand the template- 返回:
- all HTTP headers of that resource
- 抛出:
RestClientException
headForHeaders
HttpHeaders headForHeaders(String url, Map<String,?> uriVariables) throws RestClientException
Retrieve all headers of the resource specified by the URI template.URI Template variables are expanded using the given map.
- 参数:
url
- the URLuriVariables
- the map containing variables for the URI template- 返回:
- all HTTP headers of that resource
- 抛出:
RestClientException
headForHeaders
HttpHeaders headForHeaders(URI url) throws RestClientException
Retrieve all headers of the resource specified by the URL.- 参数:
url
- the URL- 返回:
- all HTTP headers of that resource
- 抛出:
RestClientException
postForLocation
URI postForLocation(String url, Object request, Object... uriVariables) throws RestClientException
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.- 参数:
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
URI postForLocation(String url, Object request, Map<String,?> uriVariables) throws RestClientException
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 request.- 参数:
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
URI postForLocation(URI url, Object request) throws RestClientException
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.- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)- 返回:
- the value for the
Location
header - 抛出:
RestClientException
- 另请参阅:
HttpEntity
postForObject
<T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) throws RestClientException
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.- 参数:
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
<T> T postForObject(String url, Object request, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
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.- 参数:
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
<T> T postForObject(URI url, Object request, Class<T> responseType) throws RestClientException
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.- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)responseType
- the type of the return value- 返回:
- the converted object
- 抛出:
RestClientException
- 另请参阅:
HttpEntity
postForEntity
<T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables) throws RestClientException
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.- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)uriVariables
- the variables to expand the template- 返回:
- the converted object
- 抛出:
RestClientException
- 从以下版本开始:
- 3.0.2
- 另请参阅:
HttpEntity
postForEntity
<T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
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.- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)uriVariables
- the variables to expand the template- 返回:
- the converted object
- 抛出:
RestClientException
- 从以下版本开始:
- 3.0.2
- 另请参阅:
HttpEntity
postForEntity
<T> ResponseEntity<T> postForEntity(URI url, Object request, Class<T> responseType) throws RestClientException
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.- 参数:
url
- the URLrequest
- the Object to be POSTed (may benull
)- 返回:
- the converted object
- 抛出:
RestClientException
- 从以下版本开始:
- 3.0.2
- 另请参阅:
HttpEntity
put
void put(String url, Object request, Object... uriVariables) throws RestClientException
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.- 参数:
url
- the URLrequest
- the Object to be PUT (may benull
)uriVariables
- the variables to expand the template- 抛出:
RestClientException
- 另请参阅:
HttpEntity
put
void put(String url, Object request, Map<String,?> uriVariables) throws RestClientException
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.- 参数:
url
- the URLrequest
- the Object to be PUT (may benull
)uriVariables
- the variables to expand the template- 抛出:
RestClientException
- 另请参阅:
HttpEntity
put
void put(URI url, Object request) throws RestClientException
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.- 参数:
url
- the URLrequest
- the Object to be PUT (may benull
)- 抛出:
RestClientException
- 另请参阅:
HttpEntity
patchForObject
<T> T patchForObject(String url, Object request, Class<T> responseType, Object... uriVariables) throws RestClientException
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.
- 参数:
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
- 从以下版本开始:
- 4.3.5
- 另请参阅:
HttpEntity
,HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
,HttpComponentsAsyncClientHttpRequestFactory
,OkHttp3ClientHttpRequestFactory
patchForObject
<T> T patchForObject(String url, Object request, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
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.
- 参数:
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
- 从以下版本开始:
- 4.3.5
- 另请参阅:
HttpEntity
,HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
,HttpComponentsAsyncClientHttpRequestFactory
,OkHttp3ClientHttpRequestFactory
patchForObject
<T> T patchForObject(URI url, Object request, Class<T> responseType) throws RestClientException
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.
- 参数:
url
- the URLrequest
- the object to be PATCHed (may benull
)responseType
- the type of the return value- 返回:
- the converted object
- 抛出:
RestClientException
- 从以下版本开始:
- 4.3.5
- 另请参阅:
HttpEntity
,HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
,HttpComponentsAsyncClientHttpRequestFactory
,OkHttp3ClientHttpRequestFactory
delete
void delete(String url, Object... uriVariables) throws RestClientException
Delete the resources at the specified URI.URI Template variables are expanded using the given URI variables, if any.
- 参数:
url
- the URLuriVariables
- the variables to expand in the template- 抛出:
RestClientException
delete
void delete(String url, Map<String,?> uriVariables) throws RestClientException
Delete the resources at the specified URI.URI Template variables are expanded using the given map.
- 参数:
url
- the URLuriVariables
- the variables to expand the template- 抛出:
RestClientException
delete
void delete(URI url) throws RestClientException
Delete the resources at the specified URL.- 参数:
url
- the URL- 抛出:
RestClientException
optionsForAllow
Set<HttpMethod> optionsForAllow(String url, Object... uriVariables) throws RestClientException
Return the value of the Allow header for the given URI.URI Template variables are expanded using the given URI variables, if any.
- 参数:
url
- the URLuriVariables
- the variables to expand in the template- 返回:
- the value of the allow header
- 抛出:
RestClientException
optionsForAllow
Set<HttpMethod> optionsForAllow(String url, Map<String,?> uriVariables) throws RestClientException
Return the value of the Allow header for the given URI.URI Template variables are expanded using the given map.
- 参数:
url
- the URLuriVariables
- the variables to expand in the template- 返回:
- the value of the allow header
- 抛出:
RestClientException
optionsForAllow
Set<HttpMethod> optionsForAllow(URI url) throws RestClientException
Return the value of the Allow header for the given URL.- 参数:
url
- the URL- 返回:
- the value of the allow header
- 抛出:
RestClientException
exchange
<T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException
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.
- 参数:
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
- 从以下版本开始:
- 3.0.2
exchange
<T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Map<String,?> uriVariables) throws RestClientException
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.
- 参数:
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
- 从以下版本开始:
- 3.0.2
exchange
<T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType) throws RestClientException
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity
.- 参数:
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
- 从以下版本开始:
- 3.0.2
exchange
<T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Object... uriVariables) throws RestClientException
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);
- 参数:
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
- 从以下版本开始:
- 3.2
exchange
<T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String,?> uriVariables) throws RestClientException
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);
- 参数:
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
- 从以下版本开始:
- 3.2
exchange
<T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException
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);
- 参数:
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
- 从以下版本开始:
- 3.2
exchange
<T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, Class<T> responseType) throws RestClientException
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);
- 参数:
requestEntity
- the entity to write to the requestresponseType
- the type of the return value- 返回:
- the response as entity
- 抛出:
RestClientException
- 从以下版本开始:
- 4.1
exchange
<T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException
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);
- 参数:
requestEntity
- the entity to write to the requestresponseType
- the type of the return value- 返回:
- the response as entity
- 抛出:
RestClientException
- 从以下版本开始:
- 4.1
execute
<T> T execute(String url, HttpMethod method, RequestCallback requestCallback, 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.
- 参数:
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
<T> T execute(String url, HttpMethod method, RequestCallback requestCallback, 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.
- 参数:
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
<T> T execute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor) throws RestClientException
Execute the HTTP method to the given URL, preparing the request with theRequestCallback
, and reading the response with aResponseExtractor
.- 参数:
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