> 文档中心 > 远程调用两个微服务接口进行拼接,以及JsonObject、JsonArray的封装、打印及非空判断

远程调用两个微服务接口进行拼接,以及JsonObject、JsonArray的封装、打印及非空判断


远程调用两个微服务接口进行拼接

//远程调用两个微服务接口进行拼接@Controller@RequestMapping("/index")@ResponseBodypublic class FeignController {String URL_PATH_1 = "http://xxxxxxx:30091/g";      String URL_PATH_2 = "http://xxxxxxx.9:30100/";    @PostMapping(value = "/getAllCountXllx")    public CommonResult<Count> index(@RequestParam(required = false) String startTime,  @RequestParam(required = false) String endTime,  @RequestParam(required = false) Long cityId,  @RequestParam(required = false) Long countryId) { Map<String, Object> param = new HashMap<>(); param.put("startTime", startTime); param.put("endTime", endTime); param.put("cityId", cityId); param.put("countryId", countryId); //JSONObject是一个map // 远程调用 1 String rs1 = HttpRequest.post(URL_PATH_1).form(param).send().charset("utf-8").bodyText(); JSONObject jsonObject01 = JSONObject.parseObject(rs1); //根据key获取到jsonObject01对象中的value值,获取到的是object类型。需要手动转化 String str1 = jsonObject01.getString("data"); JSONObject jsonObject1 = JSONObject.parseObject(str1); Map<String, Object> map1 = jsonObject1; String strVal1 = ""; //  Set<Map.Entry> entrySet();在循环便利时使用,取得时建和值的映射关系,Entry是map接口中的内部接口与string字符串转化 for (Map.Entry<String, Object> entry : map1.entrySet()) {     System.out.println(entry.getValue());     strVal1 += entry.getValue(); } // 远程调用 2 String rs2 = HttpRequest.post(URL_PATH_2).form(param).send().charset("utf-8").bodyText(); JSONObject jsonObject02 = JSONObject.parseObject(rs2); String str2 = jsonObject02.getString("data"); JSONObject jsonObject2 = JSONObject.parseObject(str2); Map<String, Object> map2 = jsonObject2; String strVal2 = ""; for (Map.Entry<String, Object> entry : map2.entrySet()) {     System.out.println(entry.getValue());     strVal2 += entry.getValue(); } Count count = new Count(); count.setCnt2(Integer.valueOf(strVal2)); count.setCnt1(Integer.valueOf(strVal1)); return new CommonResult(200, "success", count);    }}

JsonObject、JsonArray的封装、打印及非空判断

 // 远程调用 1 String rs1 = HttpRequest.post(URL_PATH_1).form(param).send().charset("utf-8").bodyText(); JSONObject jsonObject01 = JSONObject.parseObject(rs1); //根据key获取到jsonObject01对象中的value值,获取到的是object类型。需要手动转化 String str1 = jsonObject01.getString("data"); JSONObject jsonObject1 = JSONObject.parseObject(str1); Map<String, Object> map1 = jsonObject1; String strVal1 = ""; //  Set<Map.Entry> entrySet();在循环便利时使用,取得时建和值的映射关系,Entry是map接口中的内部接口与string字符串转化 for (Map.Entry<String, Object> entry : map1.entrySet()) {     System.out.println(entry.getValue());     strVal1 += entry.getValue(); }