远程调用两个微服务接口进行拼接
@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); String rs1 = HttpRequest.post(URL_PATH_1).form(param).send().charset("utf-8").bodyText(); JSONObject jsonObject01 = JSONObject.parseObject(rs1); String str1 = jsonObject01.getString("data"); JSONObject jsonObject1 = JSONObject.parseObject(str1); Map<String, Object> map1 = jsonObject1; String strVal1 = ""; for (Map.Entry<String, Object> entry : map1.entrySet()) { System.out.println(entry.getValue()); strVal1 += entry.getValue(); } 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的封装、打印及非空判断
String rs1 = HttpRequest.post(URL_PATH_1).form(param).send().charset("utf-8").bodyText(); JSONObject jsonObject01 = JSONObject.parseObject(rs1); String str1 = jsonObject01.getString("data"); JSONObject jsonObject1 = JSONObject.parseObject(str1); Map<String, Object> map1 = jsonObject1; String strVal1 = ""; for (Map.Entry<String, Object> entry : map1.entrySet()) { System.out.println(entry.getValue()); strVal1 += entry.getValue(); }