RestTemplate 调用SpringCloud服务
程序员文章站
2022-06-26 08:06:24
...
//注入rest模板
@Autowired
private RestTemplate restTemplate;
@Override
public TravelInfoF07RespM01 execute(TravelInfoF07ReqtM01 params) throws Exception {
//设置请求头信息(按照需求来 ,我这里设置了分页信息)
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.parseMediaType(MEDIATYPE));
header.add(ACCEPT, MediaType.APPLICATION_JSON.toString());
header.add("pageIndex", "0");
header.add("pageSize", "10");
header.add("orderBy", "worklistDate DESC");
//这个很重要 设置一个可用的token
header.add("Authorization", "bearereyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE4MzkzOTU1OTksInVzZXJfbmFtZSI6ImFkbWluIiwiYXV0aG9yaXRpZXMiOlsiUk9MRV9BRE1JTiJdLCJqdGkiOiJjYWFiZGVlZi1kYjNmLTQ0NjYtODMyNi0zZTNkMDA5MjcxZjEiLCJjbGllbnRfaWQiOiJrMiIsInNjb3BlIjpbIm9wZW5pZCJdfQ.ifeK4OAxv-dXxdVJslL6e7uFK4EC0W3SzVaQRmnYwDH0X7yHKIIiyHSvzMFjj8KUcJjRxiz35_JehdofQGYq8xrKpM4IQwq455PkIm0IxHRtTi-8fP8XfGb_KyTtzK_tqVw41ejIpJQbGar7sTWveR5fyR7a-vxsEmMoy5Q8wL8Bvl4Y9ToOtzz-NkkIzl2AJvKdT_jCI2C50WqdlzY_UanGvRGoNiJCt7JyfjNvKctVVQVuzfLFmmidRh3FuWDTBo_pSzqJb2K0da7Ae7l75JAiO6gMhfyIcYHnUBhyf_kZFiSzi4qeToBR-yYSnYngbAuDgdK55bwWLqMIMVqn_Q");
//这里我的理解是 将请求参数封装到这个类
HttpEntity<TravelInfoF07ReqtM01> paramsEntity = new HttpEntity<>(params,header);
//模板发送请求
//参数一是路径就不用说了 参数二就是上面这个对象也就是请求参数,参数三是指定返回值类型 属性要和调用服务的返回类型属性一致
TravelInfoF07RespM01 postForEntity = restTemplate.postForObject("http://k2platformdemo.k2software.cn:30009/user-service/api/tasks/todo", paramsEntity, TravelInfoF07RespM01.class);
//以下就是我的逻辑了
if(postForEntity.getWorklists() == null || postForEntity.getWorklists().size() < 1) {
return postForEntity;
}
//获取所有待办
List<TravelInfoF07RespS01> list = postForEntity.getWorklists();
//创建过滤后的待办
List<TravelInfoF07RespS01> list2 = new ArrayList<TravelInfoF07RespS01>();
//流水号集合
List<String> folioList = new ArrayList<>();
//过滤流程和节点
for (TravelInfoF07RespS01 travelInfoF07RespS01 : list) {
//节点名
String activityName = travelInfoF07RespS01.getActivityName().asText();
if(params.getActivityName().equals(activityName)) {
list2.add(travelInfoF07RespS01);
folioList.add(travelInfoF07RespS01.getFolio());
}
}
if(folioList == null || folioList.size() < 1){
postForEntity.setWorklists(null);
return postForEntity;
}
//查询当前流程 、节点表格数据
TravelInfoF07SQL01IM01 sql01 = new TravelInfoF07SQL01IM01();
sql01.setFolioList(folioList);
List<TravelInfoF07SQL01OM01> QueryList = queryDAO.executeForObjectList("TravelInfoF07SQL01", sql01);
//将过滤后的待办返回给响应DTO
List<TravelInfoF07RespS01> list3 = new ArrayList<>();
for (TravelInfoF07SQL01OM01 travelInfoF07SQL01OM01 : QueryList) {
for (TravelInfoF07RespS01 travelInfoF07RespS01 : list2) {
if(travelInfoF07SQL01OM01.getFolio().equals(travelInfoF07RespS01.getFolio())) {
travelInfoF07RespS01.setApplyName(travelInfoF07SQL01OM01.getApplyName());
travelInfoF07RespS01.setOwnerDept(travelInfoF07SQL01OM01.getOwnerDept());
travelInfoF07RespS01.setRmbAmount(travelInfoF07SQL01OM01.getRmbAmount());
list3.add(travelInfoF07RespS01);
}
}
}
HttpHeaders header2 = new HttpHeaders();
header2.setContentType(MediaType.parseMediaType(MEDIATYPE));
header2.add(ACCEPT, MediaType.APPLICATION_JSON.toString());
header2.add("Authorization", "bearereyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE4MzkzOTU1OTksInVzZXJfbmFtZSI6ImFkbWluIiwiYXV0aG9yaXRpZXMiOlsiUk9MRV9BRE1JTiJdLCJqdGkiOiJjYWFiZGVlZi1kYjNmLTQ0NjYtODMyNi0zZTNkMDA5MjcxZjEiLCJjbGllbnRfaWQiOiJrMiIsInNjb3BlIjpbIm9wZW5pZCJdfQ.ifeK4OAxv-dXxdVJslL6e7uFK4EC0W3SzVaQRmnYwDH0X7yHKIIiyHSvzMFjj8KUcJjRxiz35_JehdofQGYq8xrKpM4IQwq455PkIm0IxHRtTi-8fP8XfGb_KyTtzK_tqVw41ejIpJQbGar7sTWveR5fyR7a-vxsEmMoy5Q8wL8Bvl4Y9ToOtzz-NkkIzl2AJvKdT_jCI2C50WqdlzY_UanGvRGoNiJCt7JyfjNvKctVVQVuzfLFmmidRh3FuWDTBo_pSzqJb2K0da7Ae7l75JAiO6gMhfyIcYHnUBhyf_kZFiSzi4qeToBR-yYSnYngbAuDgdK55bwWLqMIMVqn_Q");
这个东西真的很好用 又简单,前面的使用都有注释 。RestTemplate 的方法有好几个可以自己百度下
上一篇: 格式工厂图片怎么转换为ICO格式?
下一篇: C#中的DLL文件
推荐阅读
-
C#中调用Windows系统服务exe程序的工具类与重启服务的流程
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码
-
SpringCloud-创建服务消费者-Feign方式(附代码下载)
-
jsp页面获取服务器时间的简单调用示例
-
实战SpringCloud响应式微服务系列教程(第六章)
-
springcloud学习之路: (四) springcloud集成Hystrix服务保护
-
Spring Cloud第七篇 | 声明式服务调用Feign
-
实战SpringCloud响应式微服务系列教程(第九章)使用Spring WebFlux构建响应式RESTful服务
-
Java--IDEA创建多服务模块的SpringCloud微服务项目
-
解决C#中调用WCF方法报错:远程服务器返回错误 (404) 未找到