类 TestRestTemplate
- java.lang.Object
 - org.springframework.boot.test.web.client.TestRestTemplate
 
public class TestRestTemplate extends Object
Convenient alternative ofRestTemplatethat is suitable for integration tests. They are fault tolerant, and optionally can carry Basic authentication headers. If Apache Http Client 4.3.2 or better is available (recommended) it will be used as the client, and by default configured to ignore cookies and redirects.Note: To prevent injection problems this class internally does not extend
RestTemplate. If you need access to the underlyingRestTemplateusegetRestTemplate().If you are using the
@SpringBootTestannotation, aTestRestTemplateis automatically available and can be@Autowiredinto your test. If you need customizations (for example to adding additional message converters) use aRestTemplateBuilder@Bean.- 从以下版本开始:
 - 1.4.0
 
嵌套类概要
嵌套类 修饰符和类型 类 说明 protected static classTestRestTemplate.CustomHttpComponentsClientHttpRequestFactoryHttpComponentsClientHttpRequestFactoryto apply customizations.static classTestRestTemplate.HttpClientOptionOptions used to customize the Apache Http Client if it is used.
构造器概要
构造器 构造器 说明 TestRestTemplate(String username, String password, TestRestTemplate.HttpClientOption... httpClientOptions)Create a newTestRestTemplateinstance with the specified credentials.TestRestTemplate(TestRestTemplate.HttpClientOption... httpClientOptions)Create a newTestRestTemplateinstance.TestRestTemplate(org.springframework.boot.web.client.RestTemplateBuilder restTemplateBuilder)Create a newTestRestTemplateinstance.TestRestTemplate(org.springframework.web.client.RestTemplate restTemplate)TestRestTemplate(org.springframework.web.client.RestTemplate restTemplate, String username, String password, TestRestTemplate.HttpClientOption... httpClientOptions)
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 voiddelete(String url, Object... urlVariables)Delete the resources at the specified URI.voiddelete(String url, Map<String,?> urlVariables)Delete the resources at the specified URI.voiddelete(URI url)Delete the resources at the specified URL.<T> org.springframework.http.ResponseEntity<T>exchange(String url, org.springframework.http.HttpMethod method, org.springframework.http.HttpEntity<?> requestEntity, Class<T> responseType, Object... urlVariables)Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity.<T> org.springframework.http.ResponseEntity<T>exchange(String url, org.springframework.http.HttpMethod method, org.springframework.http.HttpEntity<?> requestEntity, Class<T> responseType, Map<String,?> urlVariables)Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity.<T> org.springframework.http.ResponseEntity<T>exchange(String url, org.springframework.http.HttpMethod method, org.springframework.http.HttpEntity<?> requestEntity, org.springframework.core.ParameterizedTypeReference<T> responseType, Object... urlVariables)Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity.<T> org.springframework.http.ResponseEntity<T>exchange(String url, org.springframework.http.HttpMethod method, org.springframework.http.HttpEntity<?> requestEntity, org.springframework.core.ParameterizedTypeReference<T> responseType, Map<String,?> urlVariables)Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity.<T> org.springframework.http.ResponseEntity<T>exchange(URI url, org.springframework.http.HttpMethod method, org.springframework.http.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> org.springframework.http.ResponseEntity<T>exchange(URI url, org.springframework.http.HttpMethod method, org.springframework.http.HttpEntity<?> requestEntity, org.springframework.core.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> org.springframework.http.ResponseEntity<T>exchange(org.springframework.http.RequestEntity<?> requestEntity, Class<T> responseType)Execute the request specified in the givenRequestEntityand return the response asResponseEntity.<T> org.springframework.http.ResponseEntity<T>exchange(org.springframework.http.RequestEntity<?> requestEntity, org.springframework.core.ParameterizedTypeReference<T> responseType)Execute the request specified in the givenRequestEntityand return the response asResponseEntity.<T> Texecute(String url, org.springframework.http.HttpMethod method, org.springframework.web.client.RequestCallback requestCallback, org.springframework.web.client.ResponseExtractor<T> responseExtractor, Object... urlVariables)Execute the HTTP method to the given URI template, preparing the request with theRequestCallback, and reading the response with aResponseExtractor.<T> Texecute(String url, org.springframework.http.HttpMethod method, org.springframework.web.client.RequestCallback requestCallback, org.springframework.web.client.ResponseExtractor<T> responseExtractor, Map<String,?> urlVariables)Execute the HTTP method to the given URI template, preparing the request with theRequestCallback, and reading the response with aResponseExtractor.<T> Texecute(URI url, org.springframework.http.HttpMethod method, org.springframework.web.client.RequestCallback requestCallback, org.springframework.web.client.ResponseExtractor<T> responseExtractor)Execute the HTTP method to the given URL, preparing the request with theRequestCallback, and reading the response with aResponseExtractor.<T> org.springframework.http.ResponseEntity<T>getForEntity(String url, Class<T> responseType, Object... urlVariables)Retrieve an entity by doing a GET on the specified URL.<T> org.springframework.http.ResponseEntity<T>getForEntity(String url, Class<T> responseType, Map<String,?> urlVariables)Retrieve a representation by doing a GET on the URI template.<T> org.springframework.http.ResponseEntity<T>getForEntity(URI url, Class<T> responseType)Retrieve a representation by doing a GET on the URL .<T> TgetForObject(String url, Class<T> responseType, Object... urlVariables)Retrieve a representation by doing a GET on the specified URL.<T> TgetForObject(String url, Class<T> responseType, Map<String,?> urlVariables)Retrieve a representation by doing a GET on the URI template.<T> TgetForObject(URI url, Class<T> responseType)Retrieve a representation by doing a GET on the URL .org.springframework.web.client.RestTemplategetRestTemplate()Returns the underlyingRestTemplatethat is actually used to perform the REST operations.org.springframework.http.HttpHeadersheadForHeaders(String url, Object... urlVariables)Retrieve all headers of the resource specified by the URI template.org.springframework.http.HttpHeadersheadForHeaders(String url, Map<String,?> urlVariables)Retrieve all headers of the resource specified by the URI template.org.springframework.http.HttpHeadersheadForHeaders(URI url)Retrieve all headers of the resource specified by the URL.Set<org.springframework.http.HttpMethod>optionsForAllow(String url, Object... urlVariables)Return the value of the Allow header for the given URI.Set<org.springframework.http.HttpMethod>optionsForAllow(String url, Map<String,?> urlVariables)Return the value of the Allow header for the given URI.Set<org.springframework.http.HttpMethod>optionsForAllow(URI url)Return the value of the Allow header for the given URL.<T> TpatchForObject(String url, Object request, Class<T> responseType, Object... uriVariables)Update a resource by PATCHing the given object to the URI template, and returns the representation found in the response.<T> TpatchForObject(String url, Object request, Class<T> responseType, Map<String,?> uriVariables)Update a resource by PATCHing the given object to the URI template, and returns the representation found in the response.<T> TpatchForObject(URI url, Object request, Class<T> responseType)Update a resource by PATCHing the given object to the URL, and returns the representation found in the response.<T> org.springframework.http.ResponseEntity<T>postForEntity(String url, Object request, Class<T> responseType, Object... urlVariables)Create a new resource by POSTing the given object to the URI template, and returns the response asResponseEntity.<T> org.springframework.http.ResponseEntity<T>postForEntity(String url, Object request, Class<T> responseType, Map<String,?> urlVariables)Create a new resource by POSTing the given object to the URI template, and returns the response asHttpEntity.<T> org.springframework.http.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.URIpostForLocation(String url, Object request, Object... urlVariables)Create a new resource by POSTing the given object to the URI template, and returns the value of theLocationheader.URIpostForLocation(String url, Object request, Map<String,?> urlVariables)Create a new resource by POSTing the given object to the URI template, and returns the value of theLocationheader.URIpostForLocation(URI url, Object request)Create a new resource by POSTing the given object to the URL, and returns the value of theLocationheader.<T> TpostForObject(String url, Object request, Class<T> responseType, Object... urlVariables)Create a new resource by POSTing the given object to the URI template, and returns the representation found in the response.<T> TpostForObject(String url, Object request, Class<T> responseType, Map<String,?> urlVariables)Create a new resource by POSTing the given object to the URI template, and returns the representation found in the response.<T> TpostForObject(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.voidput(String url, Object request, Object... urlVariables)Create or update a resource by PUTting the given object to the URI.voidput(String url, Object request, Map<String,?> urlVariables)Creates a new resource by PUTting the given object to URI template.voidput(URI url, Object request)Creates a new resource by PUTting the given object to URL.voidsetUriTemplateHandler(org.springframework.web.util.UriTemplateHandler handler)Configure theUriTemplateHandlerto use to expand URI templates.TestRestTemplatewithBasicAuth(String username, String password)Creates a newTestRestTemplatewith the same configuration as this one, except that it will send basic authorization headers using the givenusernameandpassword.
构造器详细资料
TestRestTemplate
public TestRestTemplate(org.springframework.boot.web.client.RestTemplateBuilder restTemplateBuilder)
Create a newTestRestTemplateinstance.- 参数:
 restTemplateBuilder- builder used to configure underlyingRestTemplate- 从以下版本开始:
 - 1.4.1
 
TestRestTemplate
public TestRestTemplate(TestRestTemplate.HttpClientOption... httpClientOptions)
Create a newTestRestTemplateinstance.- 参数:
 httpClientOptions- client options to use if the Apache HTTP Client is used
TestRestTemplate
public TestRestTemplate(String username, String password, TestRestTemplate.HttpClientOption... httpClientOptions)
Create a newTestRestTemplateinstance with the specified credentials.- 参数:
 username- the username to use (ornull)password- the password (ornull)httpClientOptions- client options to use if the Apache HTTP Client is used
TestRestTemplate
public TestRestTemplate(org.springframework.web.client.RestTemplate restTemplate)
TestRestTemplate
public TestRestTemplate(org.springframework.web.client.RestTemplate restTemplate, String username, String password, TestRestTemplate.HttpClientOption... httpClientOptions)
方法详细资料
setUriTemplateHandler
public void setUriTemplateHandler(org.springframework.web.util.UriTemplateHandler handler)
Configure theUriTemplateHandlerto use to expand URI templates. By default theDefaultUriTemplateHandleris used which relies on Spring's URI template support and exposes several useful properties that customize its behavior for encoding and for prepending a common base URL. An alternative implementation may be used to plug an external URI template library.- 参数:
 handler- the URI template handler to use
getForObject
public <T> T getForObject(String url, Class<T> responseType, Object... urlVariables) throws org.springframework.web.client.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.
- 类型参数:
 T- the type of the return value- 参数:
 url- the URLresponseType- the type of the return valueurlVariables- the variables to expand the template- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error on client-side HTTP error- 另请参阅:
 RestTemplate.getForObject(String, Class, Object...)
getForObject
public <T> T getForObject(String url, Class<T> responseType, Map<String,?> urlVariables) throws org.springframework.web.client.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.
- 类型参数:
 T- the type of the return value- 参数:
 url- the URLresponseType- the type of the return valueurlVariables- the map containing variables for the URI template- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.getForObject(String, Class, Object...)
getForObject
public <T> T getForObject(URI url, Class<T> responseType) throws org.springframework.web.client.RestClientException
Retrieve a representation by doing a GET on the URL . The response (if any) is converted and returned.- 类型参数:
 T- the type of the return value- 参数:
 url- the URLresponseType- the type of the return value- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.getForObject(java.net.URI, java.lang.Class)
getForEntity
public <T> org.springframework.http.ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... urlVariables) throws org.springframework.web.client.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.
- 类型参数:
 T- the type of the return value- 参数:
 url- the URLresponseType- the type of the return valueurlVariables- the variables to expand the template- 返回:
 - the entity
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.getForEntity(java.lang.String, java.lang.Class, java.lang.Object[])
getForEntity
public <T> org.springframework.http.ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String,?> urlVariables) throws org.springframework.web.client.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.
- 类型参数:
 T- the type of the return value- 参数:
 url- the URLresponseType- the type of the return valueurlVariables- the map containing variables for the URI template- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.getForEntity(java.lang.String, java.lang.Class, java.util.Map)
getForEntity
public <T> org.springframework.http.ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws org.springframework.web.client.RestClientException
Retrieve a representation by doing a GET on the URL . The response is converted and stored in anResponseEntity.- 类型参数:
 T- the type of the return value- 参数:
 url- the URLresponseType- the type of the return value- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.getForEntity(java.net.URI, java.lang.Class)
headForHeaders
public org.springframework.http.HttpHeaders headForHeaders(String url, Object... urlVariables) throws org.springframework.web.client.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 URLurlVariables- the variables to expand the template- 返回:
 - all HTTP headers of that resource
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.headForHeaders(java.lang.String, java.lang.Object[])
headForHeaders
public org.springframework.http.HttpHeaders headForHeaders(String url, Map<String,?> urlVariables) throws org.springframework.web.client.RestClientException
Retrieve all headers of the resource specified by the URI template.URI Template variables are expanded using the given map.
- 参数:
 url- the URLurlVariables- the map containing variables for the URI template- 返回:
 - all HTTP headers of that resource
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.headForHeaders(java.lang.String, java.util.Map)
headForHeaders
public org.springframework.http.HttpHeaders headForHeaders(URI url) throws org.springframework.web.client.RestClientException
Retrieve all headers of the resource specified by the URL.- 参数:
 url- the URL- 返回:
 - all HTTP headers of that resource
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.headForHeaders(java.net.URI)
postForLocation
public URI postForLocation(String url, Object request, Object... urlVariables) throws org.springframework.web.client.RestClientException
Create a new resource by POSTing the given object to the URI template, and returns the value of theLocationheader. This header typically indicates where the new resource is stored.URI Template variables are expanded using the given URI variables, if any.
The
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 参数:
 url- the URLrequest- the Object to be POSTed, may benullurlVariables- the variables to expand the template- 返回:
 - the value for the 
Locationheader - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 HttpEntity,RestTemplate.postForLocation(java.lang.String, java.lang.Object, java.lang.Object[])
postForLocation
public URI postForLocation(String url, Object request, Map<String,?> urlVariables) throws org.springframework.web.client.RestClientException
Create a new resource by POSTing the given object to the URI template, and returns the value of theLocationheader. This header typically indicates where the new resource is stored.URI Template variables are expanded using the given map.
The
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 参数:
 url- the URLrequest- the Object to be POSTed, may benullurlVariables- the variables to expand the template- 返回:
 - the value for the 
Locationheader - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 HttpEntity,RestTemplate.postForLocation(java.lang.String, java.lang.Object, java.util.Map)
postForLocation
public URI postForLocation(URI url, Object request) throws org.springframework.web.client.RestClientException
Create a new resource by POSTing the given object to the URL, and returns the value of theLocationheader. This header typically indicates where the new resource is stored.The
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 参数:
 url- the URLrequest- the Object to be POSTed, may benull- 返回:
 - the value for the 
Locationheader - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 HttpEntity,RestTemplate.postForLocation(java.net.URI, java.lang.Object)
postForObject
public <T> T postForObject(String url, Object request, Class<T> responseType, Object... urlVariables) throws org.springframework.web.client.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
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 类型参数:
 T- the type of the return value- 参数:
 url- the URLrequest- the Object to be POSTed, may benullresponseType- the type of the return valueurlVariables- the variables to expand the template- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 HttpEntity,RestTemplate.postForObject(java.lang.String, java.lang.Object, java.lang.Class, java.lang.Object[])
postForObject
public <T> T postForObject(String url, Object request, Class<T> responseType, Map<String,?> urlVariables) throws org.springframework.web.client.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
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 类型参数:
 T- the type of the return value- 参数:
 url- the URLrequest- the Object to be POSTed, may benullresponseType- the type of the return valueurlVariables- the variables to expand the template- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 HttpEntity,RestTemplate.postForObject(java.lang.String, java.lang.Object, java.lang.Class, java.util.Map)
postForObject
public <T> T postForObject(URI url, Object request, Class<T> responseType) throws org.springframework.web.client.RestClientException
Create a new resource by POSTing the given object to the URL, and returns the representation found in the response.The
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 类型参数:
 T- the type of the return value- 参数:
 url- the URLrequest- the Object to be POSTed, may benullresponseType- the type of the return value- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 HttpEntity,RestTemplate.postForObject(java.net.URI, java.lang.Object, java.lang.Class)
postForEntity
public <T> org.springframework.http.ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... urlVariables) throws org.springframework.web.client.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
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 类型参数:
 T- the type of the return value- 参数:
 url- the URLrequest- the Object to be POSTed, may benullresponseType- the response type to returnurlVariables- the variables to expand the template- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 HttpEntity,RestTemplate.postForEntity(java.lang.String, java.lang.Object, java.lang.Class, java.lang.Object[])
postForEntity
public <T> org.springframework.http.ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Map<String,?> urlVariables) throws org.springframework.web.client.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
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 类型参数:
 T- the type of the return value- 参数:
 url- the URLrequest- the Object to be POSTed, may benullresponseType- the response type to returnurlVariables- the variables to expand the template- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 HttpEntity,RestTemplate.postForEntity(java.lang.String, java.lang.Object, java.lang.Class, java.util.Map)
postForEntity
public <T> org.springframework.http.ResponseEntity<T> postForEntity(URI url, Object request, Class<T> responseType) throws org.springframework.web.client.RestClientException
Create a new resource by POSTing the given object to the URL, and returns the response asResponseEntity.The
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 类型参数:
 T- the type of the return value- 参数:
 url- the URLrequest- the Object to be POSTed, may benullresponseType- the response type to return- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 HttpEntity,RestTemplate.postForEntity(java.net.URI, java.lang.Object, java.lang.Class)
put
public void put(String url, Object request, Object... urlVariables) throws org.springframework.web.client.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
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 参数:
 url- the URLrequest- the Object to be PUT, may benullurlVariables- the variables to expand the template- 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 HttpEntity,RestTemplate.put(java.lang.String, java.lang.Object, java.lang.Object[])
put
public void put(String url, Object request, Map<String,?> urlVariables) throws org.springframework.web.client.RestClientException
Creates a new resource by PUTting the given object to URI template.URI Template variables are expanded using the given map.
The
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 参数:
 url- the URLrequest- the Object to be PUT, may benullurlVariables- the variables to expand the template- 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 HttpEntity,RestTemplate.put(java.lang.String, java.lang.Object, java.util.Map)
put
public void put(URI url, Object request) throws org.springframework.web.client.RestClientException
Creates a new resource by PUTting the given object to URL.The
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 参数:
 url- the URLrequest- the Object to be PUT, may benull- 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 HttpEntity,RestTemplate.put(java.net.URI, java.lang.Object)
patchForObject
public <T> T patchForObject(String url, Object request, Class<T> responseType, Object... uriVariables) throws org.springframework.web.client.RestClientException
Update a resource by PATCHing 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
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 类型参数:
 T- the type of the return value- 参数:
 url- the URLrequest- the Object to be PATCHed, may benullresponseType- the type of the return valueuriVariables- the variables to expand the template- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 从以下版本开始:
 - 1.4.4
 - 另请参阅:
 HttpEntity
patchForObject
public <T> T patchForObject(String url, Object request, Class<T> responseType, Map<String,?> uriVariables) throws org.springframework.web.client.RestClientException
Update a resource by PATCHing 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
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 类型参数:
 T- the type of the return value- 参数:
 url- the URLrequest- the Object to be PATCHed, may benullresponseType- the type of the return valueuriVariables- the variables to expand the template- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 从以下版本开始:
 - 1.4.4
 - 另请参阅:
 HttpEntity
patchForObject
public <T> T patchForObject(URI url, Object request, Class<T> responseType) throws org.springframework.web.client.RestClientException
Update a resource by PATCHing the given object to the URL, and returns the representation found in the response.The
requestparameter can be aHttpEntityin order to add additional HTTP headers to the request.- 类型参数:
 T- the type of the return value- 参数:
 url- the URLrequest- the Object to be POSTed, may benullresponseType- the type of the return value- 返回:
 - the converted object
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 从以下版本开始:
 - 1.4.4
 - 另请参阅:
 HttpEntity
delete
public void delete(String url, Object... urlVariables) throws org.springframework.web.client.RestClientException
Delete the resources at the specified URI.URI Template variables are expanded using the given URI variables, if any.
- 参数:
 url- the URLurlVariables- the variables to expand in the template- 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.delete(java.lang.String, java.lang.Object[])
delete
public void delete(String url, Map<String,?> urlVariables) throws org.springframework.web.client.RestClientException
Delete the resources at the specified URI.URI Template variables are expanded using the given map.
- 参数:
 url- the URLurlVariables- the variables to expand the template- 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.delete(java.lang.String, java.util.Map)
delete
public void delete(URI url) throws org.springframework.web.client.RestClientException
Delete the resources at the specified URL.- 参数:
 url- the URL- 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.delete(java.net.URI)
optionsForAllow
public Set<org.springframework.http.HttpMethod> optionsForAllow(String url, Object... urlVariables) throws org.springframework.web.client.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 URLurlVariables- the variables to expand in the template- 返回:
 - the value of the allow header
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.optionsForAllow(java.lang.String, java.lang.Object[])
optionsForAllow
public Set<org.springframework.http.HttpMethod> optionsForAllow(String url, Map<String,?> urlVariables) throws org.springframework.web.client.RestClientException
Return the value of the Allow header for the given URI.URI Template variables are expanded using the given map.
- 参数:
 url- the URLurlVariables- the variables to expand in the template- 返回:
 - the value of the allow header
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.optionsForAllow(java.lang.String, java.util.Map)
optionsForAllow
public Set<org.springframework.http.HttpMethod> optionsForAllow(URI url) throws org.springframework.web.client.RestClientException
Return the value of the Allow header for the given URL.- 参数:
 url- the URL- 返回:
 - the value of the allow header
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.optionsForAllow(java.net.URI)
exchange
public <T> org.springframework.http.ResponseEntity<T> exchange(String url, org.springframework.http.HttpMethod method, org.springframework.http.HttpEntity<?> requestEntity, Class<T> responseType, Object... urlVariables) throws org.springframework.web.client.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.
- 类型参数:
 T- the type of the return value- 参数:
 url- the URLmethod- the HTTP method (GET, POST, etc)requestEntity- the entity (headers and/or body) to write to the request, may benullresponseType- the type of the return valueurlVariables- the variables to expand in the template- 返回:
 - the response as entity
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.exchange(java.lang.String, org.springframework.http.HttpMethod, org.springframework.http.HttpEntity, java.lang.Class, java.lang.Object[])
exchange
public <T> org.springframework.http.ResponseEntity<T> exchange(String url, org.springframework.http.HttpMethod method, org.springframework.http.HttpEntity<?> requestEntity, Class<T> responseType, Map<String,?> urlVariables) throws org.springframework.web.client.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.
- 类型参数:
 T- the type of the return value- 参数:
 url- the URLmethod- the HTTP method (GET, POST, etc)requestEntity- the entity (headers and/or body) to write to the request, may benullresponseType- the type of the return valueurlVariables- the variables to expand in the template- 返回:
 - the response as entity
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.exchange(java.lang.String, org.springframework.http.HttpMethod, org.springframework.http.HttpEntity, java.lang.Class, java.util.Map)
exchange
public <T> org.springframework.http.ResponseEntity<T> exchange(URI url, org.springframework.http.HttpMethod method, org.springframework.http.HttpEntity<?> requestEntity, Class<T> responseType) throws org.springframework.web.client.RestClientException
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity.- 类型参数:
 T- the type of the return value- 参数:
 url- the URLmethod- the HTTP method (GET, POST, etc)requestEntity- the entity (headers and/or body) to write to the request, may benullresponseType- the type of the return value- 返回:
 - the response as entity
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.exchange(java.net.URI, org.springframework.http.HttpMethod, org.springframework.http.HttpEntity, java.lang.Class)
exchange
public <T> org.springframework.http.ResponseEntity<T> exchange(String url, org.springframework.http.HttpMethod method, org.springframework.http.HttpEntity<?> requestEntity, org.springframework.core.ParameterizedTypeReference<T> responseType, Object... urlVariables) throws org.springframework.web.client.RestClientException
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity. The givenParameterizedTypeReferenceis used to pass generic type information:ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {}; ResponseEntity<List<MyBean>> response = template.exchange("http://example.com",HttpMethod.GET, null, myBean);- 类型参数:
 T- the type of the return value- 参数:
 url- the URLmethod- the HTTP method (GET, POST, etc)requestEntity- the entity (headers and/or body) to write to the request, may benullresponseType- the type of the return valueurlVariables- the variables to expand in the template- 返回:
 - the response as entity
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.exchange(java.lang.String, org.springframework.http.HttpMethod, org.springframework.http.HttpEntity, org.springframework.core.ParameterizedTypeReference, java.lang.Object[])
exchange
public <T> org.springframework.http.ResponseEntity<T> exchange(String url, org.springframework.http.HttpMethod method, org.springframework.http.HttpEntity<?> requestEntity, org.springframework.core.ParameterizedTypeReference<T> responseType, Map<String,?> urlVariables) throws org.springframework.web.client.RestClientException
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity. The givenParameterizedTypeReferenceis used to pass generic type information:ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {}; ResponseEntity<List<MyBean>> response = template.exchange("http://example.com",HttpMethod.GET, null, myBean);- 类型参数:
 T- the type of the return value- 参数:
 url- the URLmethod- the HTTP method (GET, POST, etc)requestEntity- the entity (headers and/or body) to write to the request, may benullresponseType- the type of the return valueurlVariables- the variables to expand in the template- 返回:
 - the response as entity
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.exchange(java.lang.String, org.springframework.http.HttpMethod, org.springframework.http.HttpEntity, org.springframework.core.ParameterizedTypeReference, java.util.Map)
exchange
public <T> org.springframework.http.ResponseEntity<T> exchange(URI url, org.springframework.http.HttpMethod method, org.springframework.http.HttpEntity<?> requestEntity, org.springframework.core.ParameterizedTypeReference<T> responseType) throws org.springframework.web.client.RestClientException
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response asResponseEntity. The givenParameterizedTypeReferenceis used to pass generic type information:ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {}; ResponseEntity<List<MyBean>> response = template.exchange("http://example.com",HttpMethod.GET, null, myBean);- 类型参数:
 T- the type of the return value- 参数:
 url- the URLmethod- the HTTP method (GET, POST, etc)requestEntity- the entity (headers and/or body) to write to the request, may benullresponseType- the type of the return value- 返回:
 - the response as entity
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.exchange(java.net.URI, org.springframework.http.HttpMethod, org.springframework.http.HttpEntity, org.springframework.core.ParameterizedTypeReference)
exchange
public <T> org.springframework.http.ResponseEntity<T> exchange(org.springframework.http.RequestEntity<?> requestEntity, Class<T> responseType) throws org.springframework.web.client.RestClientException
Execute the request specified in the givenRequestEntityand return the response asResponseEntity. Typically used in combination with the static builder methods onRequestEntity, for instance:MyRequest body = ... RequestEntity request = RequestEntity.post(new URI("http://example.com/foo")).accept(MediaType.APPLICATION_JSON).body(body); ResponseEntity<MyResponse> response = template.exchange(request, MyResponse.class);- 类型参数:
 T- the type of the return value- 参数:
 requestEntity- the entity to write to the requestresponseType- the type of the return value- 返回:
 - the response as entity
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.exchange(org.springframework.http.RequestEntity, java.lang.Class)
exchange
public <T> org.springframework.http.ResponseEntity<T> exchange(org.springframework.http.RequestEntity<?> requestEntity, org.springframework.core.ParameterizedTypeReference<T> responseType) throws org.springframework.web.client.RestClientException
Execute the request specified in the givenRequestEntityand return the response asResponseEntity. The givenParameterizedTypeReferenceis used to pass generic type information:MyRequest body = ... RequestEntity request = RequestEntity.post(new URI("http://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);- 类型参数:
 T- the type of the return value- 参数:
 requestEntity- the entity to write to the requestresponseType- the type of the return value- 返回:
 - the response as entity
 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.exchange(org.springframework.http.RequestEntity, org.springframework.core.ParameterizedTypeReference)
execute
public <T> T execute(String url, org.springframework.http.HttpMethod method, org.springframework.web.client.RequestCallback requestCallback, org.springframework.web.client.ResponseExtractor<T> responseExtractor, Object... urlVariables) throws org.springframework.web.client.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.
- 类型参数:
 T- the type of the return value- 参数:
 url- the URLmethod- the HTTP method (GET, POST, etc)requestCallback- object that prepares the requestresponseExtractor- object that extracts the return value from the responseurlVariables- the variables to expand in the template- 返回:
 - an arbitrary object, as returned by the 
ResponseExtractor - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.execute(java.lang.String, org.springframework.http.HttpMethod, org.springframework.web.client.RequestCallback, org.springframework.web.client.ResponseExtractor, java.lang.Object[])
execute
public <T> T execute(String url, org.springframework.http.HttpMethod method, org.springframework.web.client.RequestCallback requestCallback, org.springframework.web.client.ResponseExtractor<T> responseExtractor, Map<String,?> urlVariables) throws org.springframework.web.client.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.
- 类型参数:
 T- the type of the return value- 参数:
 url- the URLmethod- the HTTP method (GET, POST, etc)requestCallback- object that prepares the requestresponseExtractor- object that extracts the return value from the responseurlVariables- the variables to expand in the template- 返回:
 - an arbitrary object, as returned by the 
ResponseExtractor - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.execute(java.lang.String, org.springframework.http.HttpMethod, org.springframework.web.client.RequestCallback, org.springframework.web.client.ResponseExtractor, java.util.Map)
execute
public <T> T execute(URI url, org.springframework.http.HttpMethod method, org.springframework.web.client.RequestCallback requestCallback, org.springframework.web.client.ResponseExtractor<T> responseExtractor) throws org.springframework.web.client.RestClientException
Execute the HTTP method to the given URL, preparing the request with theRequestCallback, and reading the response with aResponseExtractor.- 类型参数:
 T- the type of the return value- 参数:
 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 - 抛出:
 org.springframework.web.client.RestClientException- on client-side HTTP error- 另请参阅:
 RestTemplate.execute(java.net.URI, org.springframework.http.HttpMethod, org.springframework.web.client.RequestCallback, org.springframework.web.client.ResponseExtractor)
getRestTemplate
public org.springframework.web.client.RestTemplate getRestTemplate()
Returns the underlyingRestTemplatethat is actually used to perform the REST operations.- 返回:
 - the restTemplate
 
withBasicAuth
public TestRestTemplate withBasicAuth(String username, String password)
Creates a newTestRestTemplatewith the same configuration as this one, except that it will send basic authorization headers using the givenusernameandpassword.- 参数:
 username- the usernamepassword- the password- 返回:
 - the new template
 - 从以下版本开始:
 - 1.4.1