服务B的接收接口
@ApiOperation("根据教研室corpIdList查询教研室的成员数量")
@PostMapping("/user/count")
public AjaxResult getRoomUserCount(@RequestBody List<String> corpIdList) {
List<JxClassroomUserCountVo> voList = classroomService.getRoomUserCount(corpIdList);
return AjaxResult.success(voList);
}
服务A调用方
List<String> corpIdList = jxClassroomList.stream().map(JxClassroom::getCorpId).collect(Collectors.toList());
String jsonString = JSONObject.toJSONString(corpIdList);
String info = HttpUtil.sendPost(DataUrl + "/room/user/count", jsonString);
这个是封装好的发送post请求方法
/**
* 发送HttpPost请求,参数为json字符串
*
* @param url 请求地址
* @param jsonStr json字符串
* @return
*/
public static String sendPost(String url, String jsonStr) {
CloseableHttpClient httpclient = getBuilder().build();
HttpPost httpPost = new HttpPost(url);
String result = null;
try {
StringEntity stringEntity = new StringEntity(jsonStr, Consts.UTF_8);
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}