企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
RestTemplate是Spring提供的一个访问Http服务的客户端类。从名称上来看,该类更多是针对RESTFUL风格API设计的。RestTemplate的底层实现仍然是HttpClient或HttpUrlConnection或OkHttp(三者可选),只是对它进行了封装,从而降低编码复杂度。 ![](https://img.kancloud.cn/68/f1/68f1c5fbc7ea23b135dfbd694a6ca21a_283x198.png) ## 一、RestTemplate常用方法 RestTemplate提供的常用方法是以Http协议中的6个动词开头的: | HTTP Method | 常用方法 | 描述 | | --- | --- | --- | | GET | getForObject | 发起GET请求响应对象 | | GET | getForEntity | 发起GET请求响应结果、包含响应对象、请求头、状态码等HTTP协议详细内容 | | POST | postForObject | 发起POST请求响应对象 | | POST | postForEntity | 发起POST请求响应结果、包含响应对象、请求头、状态码等HTTP协议详细内容 | | DELETE | delete | 发起HTTP的DELETE方法请求 | | PUT | put | 发起HTTP的PUT方法请求 | 这些方法的名称清楚地表明它们调用的是哪个HTTP方法,而名称中包含的第二部分表示返回的内容。 ## 二、远程服务调用 要使用RestTemplate ,必须是Spring环境,首先将它初始化为一个Bean。只做一次即可。 ~~~ @Configuration public class ContextConfig { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } } ~~~ 下面的Junit代码实现了使用RestTemplate发送Post请求,到“/sms/send”短信发送服务。从代码的实现复杂度上已经比使用HttpClient要简单了许多。 ~~~ @ExtendWith(SpringExtension.class) //Junit5 @SpringBootTest public class RestTemplateTest { @Resource private RestTemplate restTemplate; @Test void httpPostForObject() throws Exception { //发送远程http请求的url String url = "http://localhost:8402/sms/send"; //发送到远程服务的参数 MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); params.add("phoneNo", "13214409773"); params.add("content", "HttpClient测试远程服务调用"); //通过RestTemplate对象发送post请求 AjaxResponse ajaxResponse = restTemplate.postForObject(url, params, AjaxResponse.class); System.out.println(ajaxResponse); } } ~~~ Junit测试控制台打印(参考aservice-sms的“/sms/send”的服务定义): ![](https://img.kancloud.cn/35/a0/35a01bbe201c68256fb64706afbd66af_1071x53.png) 如果我们想获取请求结果中的更多Http协议信息,可以使用postForEntity方法。如下: ~~~ @Test void httpPostForEntity() throws Exception { //发送远程http请求的url String url = "http://localhost:8402/sms/send"; //发送到远程服务的参数 MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); params.add("phoneNo", "13214409773"); params.add("content", "HttpClient测试远程服务调用"); //通过RestTemplate对象发送post请求 ResponseEntity<AjaxResponse> entitys = restTemplate.postForEntity(url, params, AjaxResponse.class); System.out.println(entitys.getBody()); //查看响应的状态码 System.out.println(entitys.getStatusCodeValue()); //查看响应头 HttpHeaders headMap = entitys.getHeaders(); for(Map.Entry<String, List<String>> m : headMap.entrySet()){ System.out.println(m.getKey() + ": " + m.getValue()); } } ~~~ Junit测试控制台打印(参考aservice-sms的“/sms/send”的服务定义): ![](https://img.kancloud.cn/f1/c1/f1c121744fdac69de31302b557ed3632_1042x208.png) ## 三、RestTemplate底层实现的切换 RestTemplate底层实现最常用的有以下三种: * SimpleClientHttpRequestFactory(封装URLConnection,JDK自带,默认实现) * HttpComponentsClientHttpRequestFactory(封装第三方类库HttpClient) * OkHttp3ClientHttpRequestFactory(封装封装第三方类库OKHttp) 通常情况下,网上的资料认为OKHttp是目前执行效率最高的HTTP类库(笔者没实际测试过)。也就是下文代码中的最后一种。 ~~~ @Configuration public class ContextConfig { //默认实现,实际与urlConnection是一样的底层实现 @Bean public RestTemplate restTemplate(){ RestTemplate restTemplate = new RestTemplate(); return restTemplate; } //默认实现 @Bean("urlConnection") public RestTemplate urlConnectionRestTemplate(){ RestTemplate restTemplate = new RestTemplate(new SimpleClientHttpRequestFactory()); return restTemplate; } @Bean("httpClient") public RestTemplate httpClientRestTemplate(){ RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory()); return restTemplate; } @Bean("OKHttp3") public RestTemplate OKHttp3RestTemplate(){ RestTemplate restTemplate = new RestTemplate(new OkHttp3ClientHttpRequestFactory()); return restTemplate; } } ~~~ 在实际的应用中,只需要选择上面的代码中的其中一种RestTemplate Bean即可。