Java服务之间直接发送http请求应该怎么做?

//使用springboot-web组件中的RestTemplate
RestTemplate restTemplate = new RestTemplate();
//getForEntity方法需要用到的参数
Map<String,String> params=new HashMap<>();
params.put("applicationNumber",applicationNumber);

try{
	ResponseEntity<ServiceApiResult> result = restTemplate.getForEntity(url+"/v1/api/getBatchTradeList?applicationNumber={applicationNumber}", ServiceApiResult.class, params);
            if(result.getBody() != null && result.getBody().getData() != null){
            //将字符串类型的数组转换为list集合
                JSONArray array = JSONArray.parseArray(JSON.toJSONString(result.getBody().getData()));
                List<BatchTradeDtlDto> batchTradeDtlDtos = JSONObject.parseArray(array.toJSONString(),BatchTradeDtlDto.class);
                //遍历list集合
                for(BatchTradeDtlDto batchTradeDtlDto:batchTradeDtlDtos){
                    CutPaymentInfo cutPaymentInfo = this.entityToModel(batchTradeDtlDto);
                    list.add(cutPaymentInfo);
                }
            }else{
                log.info("未获取到数据");
            }
        }catch(Exception e){
            log.error(e.getMessage());
        }
发布了12 篇原创文章 · 获赞 1 · 访问量 1057

猜你喜欢

转载自blog.csdn.net/Genjicoo/article/details/103287034
今日推荐