# restTemplate **Repository Path**: xiyg_admin/rest-template ## Basic Information - **Project Name**: restTemplate - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-05-18 - **Last Updated**: 2024-05-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # [restTemplate](https://blog.csdn.net/itguangit/article/details/78825505) - [https://github.com/aalansehaiyang/spring-web-learning?tab=readme-ov-file](https://github.com/aalansehaiyang/spring-web-learning?tab=readme-ov-file) - [httpclientutil](https://github.com/JourWon/httpclientutil) - [webclient](https://www.callicoder.com/spring-5-reactive-webclient-webtestclient-examples/) #### 介绍 详解 RestTemplate 操作 作为开发人员,我们经常关注于构建伟大的软件来解决业务问题。数据只是软件完成工作时 要处理的原材料。但是如果你问一下业务人员,数据和软件谁更重要的话,他们很可能会选择 数据。数据是许多业务的生命之血。软件通常是可以替换的,但是多年积累的数据是永远不能 替换的。 近几年来,以信息为中心的表述性状态转移(Representational State Transfer,REST)已经称为替代传统SOAP Web 服务的流行方案. SOAP关注的一般是行为和处理,而REST关注的是要处理的数据. 从Spring3.0开始,Spring为创建Rest API提供了良好的支持. REST提供了一个更简单的可选方案。另外,很多的现代化应用都会有移动或富JavaScript客户端,它们都会使用运行在服务器上REST API。 REST的基础知识 参考我的这篇文章: Restful API 设计指南 当谈论REST时,有一种常见的错误就是将其视为“基于URL的Web服务”——将REST作为另一 种类型的远程过程调用(remote procedure call,RPC)机制,就像SOAP一样,只不过是通过简单 的HTTP URL来触发,而不是使用SOAP大量的XML命名空间 恰好相反,REST与RPC几乎没有任何关系。RPC是面向服务的,并关注于行为和动作;而REST 是面向资源的,强调描述应用程序的事物和名词。 更简洁地讲,REST就是将资源的状态以最适合客户端或服务端的形式从服务器端转移到客户 端(或者反过来)。 在REST中,资源通过URL进行识别和定位。至于RESTful URL的结构并没有严格的规则,但是 URL应该能够识别资源,而不是简单的发一条命令到服务器上。再次强调,关注的核心是事 物,而不是行为。 Spring 中如何使用Rest资源 借助 RestTemplate,Spring应用能够方便地使用REST资源 Spring的 RestTemplate访问使用了模版方法的设计模式. 模版方法将过程中与特定实现相关的部分委托给接口,而这个接口的不同实现定义了接口的不同行为. RestTemplate定义了36个与REST资源交互的方法,其中的大多数都对应于HTTP的方法。 其实,这里面只有11个独立的方法,其中有十个有三种重载形式,而第十一个则重载了六次,这样一共形成了36个方法。 delete() 在特定的URL上对资源执行HTTP DELETE操作 exchange() 在URL上执行特定的HTTP方法,返回包含对象的ResponseEntity,这个对象是从响应体中 映射得到的 execute() 在URL上执行特定的HTTP方法,返回一个从响应体映射得到的对象 getForEntity() 发送一个HTTP GET请求,返回的ResponseEntity包含了响应体所映射成的对象 getForObject() 发送一个HTTP GET请求,返回的请求体将映射为一个对象 postForEntity() POST 数据到一个URL,返回包含一个对象的ResponseEntity,这个对象是从响应体中映射得 到的 postForObject() POST 数据到一个URL,返回根据响应体匹配形成的对象 headForHeaders() 发送HTTP HEAD请求,返回包含特定资源URL的HTTP头 optionsForAllow() 发送HTTP OPTIONS请求,返回对特定URL的Allow头信息 postForLocation() POST 数据到一个URL,返回新创建资源的URL put() PUT 资源到特定的URL 实际上,由于Post 操作的非幂等性,它几乎可以代替其他的CRUD操作. #### 软件架构 软件架构说明 #### 安装教程 1. 初始化 ``` package com.dryport.tms.report.utils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplate1 { @Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory) { return new RestTemplate(factory); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory() { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(10000);//ms factory.setConnectTimeout(15000);//ms return factory; } } ``` 2. get,post请求 ``` package com.dryport.tms.report.utils; import com.dryport.common.core.web.domain.AjaxResult; import com.dryport.common.core.web.page.TableDataInfo; import com.dryport.tms.report.domain.ZbsReportBatch; import com.dryport.tms.report.domain.ZbsReportDetail; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.List; import java.util.Map; @Slf4j @RestController public class TestTemplate { @Autowired //RestTemplateBuilder private RestTemplate restTemplate; @RequestMapping("getForEntity") public List getAll2() { ResponseEntity responseEntity = restTemplate.getForEntity("http://localhost:9999/report/list", Map.class); HttpHeaders headers = responseEntity.getHeaders(); HttpStatus statusCode = responseEntity.getStatusCode(); int code = statusCode.value(); System.out.println(responseEntity); System.out.println(responseEntity.getHeaders()); System.out.println(responseEntity.getBody()); HashMap map = (HashMap) responseEntity.getBody(); System.out.println(map.get("rows")); List list = (List) map.get("rows"); return list; } //有参数的 getForEntity 请求,参数列表 @RequestMapping("getForEntity/{id}") public ZbsReportBatch getById2(@PathVariable(name = "id") String id) { ResponseEntity responseEntity = restTemplate.getForEntity("http://localhost:9999/report/{id}", ZbsReportBatch.class, id); ZbsReportBatch userEntity = responseEntity.getBody(); return userEntity; } //有参数的 get 请求,使用map封装参数 @RequestMapping("getForEntity1/{id}") public ZbsReportBatch getById4(@PathVariable(name = "id") String id) { HashMap map = new HashMap<>(); map.put("id",id); ResponseEntity responseEntity = restTemplate.getForEntity("http://localhost:9999/report/{id}", ZbsReportBatch.class, map); ZbsReportBatch userEntity = responseEntity.getBody(); return userEntity; } //无参数的 getForObject 请求 @RequestMapping("getAll2") public List getAll() { HashMap map = (HashMap) restTemplate.getForObject("http://localhost:9999/report/list", Map.class); List list = (List) map.get("rows"); return list; } //有参数的 getForObject 请求 @RequestMapping("get2/{id}") public HashMap getById(@PathVariable(name = "id") String id) { HashMap map = (HashMap) restTemplate.getForObject("http://localhost:9999/report/{id}", Map.class, id); System.out.println(map.toString()); System.out.println(map.get("data").toString()); HashMap obj = (HashMap) map.get("data"); return obj; } //有参数的 get 请求,使用map封装请求参数 @RequestMapping("get3/{id}") public HashMap getById3(@PathVariable(name = "id") String id) { HashMap map1 = new HashMap<>(); map1.put("id",id); HashMap map = (HashMap) restTemplate.getForObject("http://localhost:9999/report/{id}", Map.class, map1); System.out.println(map.toString()); System.out.println(map.get("data").toString()); HashMap obj = (HashMap) map.get("data"); return obj; } //post 请求,提交 UserEntity 对像 @RequestMapping("saveReport") public String save(@RequestBody ZbsReportDetail userEntity) { System.out.println(userEntity); ResponseEntity responseEntity = restTemplate.postForEntity("http://localhost:9999/reportDetail", userEntity, String.class); String body = responseEntity.getBody(); return body; } //post 请求,提交 UserEntity 对像 @RequestMapping("updateReport/{id}") public String updateReport(@RequestBody ZbsReportDetail userEntity, @PathVariable(name = "id") String id) { System.out.println(userEntity); userEntity.setId(Long.valueOf(id)); ResponseEntity responseEntity = restTemplate.postForEntity("http://localhost:9999/reportDetail/quash", userEntity, String.class); String body = responseEntity.getBody(); return body; } @RequestMapping("updateReport1/{id}") public String updateReport1(@RequestBody ZbsReportDetail userEntity, @PathVariable(name = "id") String id) { HashMap map = new HashMap<>(); map.put("id", id); System.out.println(userEntity); userEntity.setId(Long.valueOf(id)); ResponseEntity responseEntity = restTemplate.postForEntity("http://localhost:9999/reportDetail/quash", userEntity, String.class); String body = responseEntity.getBody(); return body; } } ``` 3. 添加请求头 #### 使用说明 1. xxxx 2. xxxx 3. xxxx #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)