欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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 的方法有好几个可以自己百度下