参数

后台调用
String url = "http://localhost:19091/api/largescreen/updateSocket";
JSONObject jsonObject = new JSONObject();
JSONObject json = new JSONObject();
JSONObject json1 = new JSONObject();
JSONObject json2 = new JSONObject();
JSONObject json3 = new JSONObject();
JSONObject json4 = new JSONObject();
JSONObject json5 = new JSONObject();
jsonObject.put("userId","1");
jsonObject.put("groupId","3");
jsonObject.put("msgType","TALK");
json.put("alarmStatistics",json1);
json.put("lineData",json2);
json.put("alarmListMap",json3);
json.put("alarmListLimit",json4);
json.put("streetInfor",json5);
String msg = json.toJSONString();
jsonObject.put("msg",msg);
String sendData = jsonObject.toJSONString();
httpUtils.httpPost(sendData,url);
接口(参数体前面加@RequestBody 变json)
@RequestMapping("/updateSocket")
@ResponseBody
public void dealMsg(@RequestBody NettyMsgDto nettyMsgDto) throws Exception {
参数体类
public class NettyMsgDto {
private String msgType;
private String groupId;
private String userId;
private String msg;
}
接口调用方法
public String httpPost(String sendData, String url) {
if (!hasInit) {
init();
}
String result = null;
try {
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(sendData, "UTF-8"));
httpPost.setConfig(requestConfig);
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
InputStream stream = httpEntity.getContent();
String resData = IOUtils.toString(stream, "UTF-8");
LOGGER.info("res data--->:{}", resData);
result = resData;
}
} finally {
response.close();
}
} catch (Exception e) {
LOGGER.error("http错误:{}", e);
}
return result;
}
接口调用要导的包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>