在前面SpringCloud之整合ribbon中,我通过RestTemplate实现了多个服务之前的接口调用,在实际开发业务中,我们也会遇到请求第三方接口的需求,这个时候就可以用上RestTemplate。
一、RestTemplate是什么
RestTemplate是Spring从3.0版本开始提供的访问远程Http服务的工具,提供了常见的REST请求方案的模版,能够大大提高客户端的编写效率。
RestTemplate默认依赖jdk的HTTP连接工具。当然你也可以 通过setRequestFactory属性切换到不同的HTTP源,比如Apache HttpComponents、Netty和OkHttp。
二、配置方法
在Spring中使用RestTemplate,需要将RestTemplate交给工厂管理,主要为以下两种方法:
三、常用方法
常用请求主要分为以下四种:
3.1 GET请求
发送GET请求用来获取资源。
3.1.1 getForEntity
返回值是一个ResponseEntity<T>,ResponseEntity是Spring对HTTP请求响应的封装。
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables)
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables
<T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType)
responseEntity.getBody();
responseEntity.getStatusCode();
responseEntity.getStatusCodeValue();
responseEntity.getHeaders()
传递参数主要为以下两种:
- 通过占位符实现
getForEntity("url?字段={数字}","返回对象的类","占位符对应的参数...(可变长度)")
getForEntity("url?字段={参占位符名字}","返回对象的类","存放参数的map")
- 通过UriComponents来构建Uri传递参数(不是String)
UriComponents uriComponents = UriComponentsBuilder.fromUriString("url?字段={占位符}").build().expand("参数).encode();
3.1.2 getForObject
是对getForEntity的进一步封装,只返回消息体
<T> T getForObject(String url, Class<T> responseType, Object... uriVariables)
<T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables
<T> T getForObject(URI url, Class<T> responseType)
传递参数方式与getForEntity一致
3.2 POST请求
发送POST请求用来新建资源或者更新资源
controller类通过 @RequestBody 注解接收请求体
3.2.1 postForEntity
和get请求的getForEntity相似
<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables)
<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables
<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType)
传递参数与getForEntity的一致
3.2.3 postForObject
是对postForEntity的进一步封装,只返回消息体
<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables)
<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables
<T> T postForObject(URI url, @Nullable Object request, Class<T> responseType)
用法和getForObject一致。
3.2.3 postForLocation
提交资源功之后,返回新资源的URI
URI postForLocation(String url, @Nullable Object request, Object... uriVariables)
URI postForLocation(String url, @Nullable Object request, Map<String, ?> uriVariables)
URI postForLocation(URI url, @Nullable Object request)
3.3 PUT请求
发送PUT请求用来更新资源
put方法没有返回值
void put(String url, @Nullable Object request, Object... uriVariables)
void put(String url, @Nullable Object request, Map<String, ?> uriVariables)
void put(URI url, @Nullable Object request)
3.4 DELETE请求
发送DELETE请求用来删除资源
void delete(String url, Object... uriVariables)
void delete(String url, Map<String, ?> uriVariables)
void delete(URI url)
3.5 补充
除了上面四种请求,redisTemplate还提供了exchange和execute两种请求方法,这两种使用范围更广,允许调用者指定HTTP请求的方法
3.5.1 exchange方法
- 允许调用者指定HTTP请求的方法(GET,POST,PUT等)
- 可以在请求中增加body以及头信息,其内容通过参数‘HttpEntity<?>requestEntity’描述
- exchange支持‘含参数的类型’(即泛型类)作为返回类型,该特性通过‘ParameterizedTypeReferenceresponseType’描述
<T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables)
<T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables)
<T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType)
<T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Object... uriVariables)
<T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String, ?> uriVariables)
<T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType)
<T> ResponseEntity<T> exchange(RequestEntity<?> entity, Class<T> responseType)
<T> ResponseEntity<T> exchange(RequestEntity<?> entity, ParameterizedTypeReference<T> responseType)
3.5.2 execute方法
上述的getForXXX、postForXXX、put、exchange、delete的最后都是execute方法,例如:
@Override
public <T> T getForObject(String url, Class<T> responseType, Object... urlVariables) throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
HttpMessageConverterExtractor<T> responseExtractor =
<span style="white-space:pre"> </span>new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.GET, requestCallback, responseExtractor, urlVariables);
}
<T> T execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, Object... uriVariables)
<T> T execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, Map<String, ?> uriVariables)
|