? ? ? ? 实现程序内接口接收多层级的复杂json数据,记录此贴。
1.接口代码设计
service方法:
/**
* 接收数据(web接口)
*
*/
public String RecData(JSONObject crossData) {
OperationLog operationLog = new OperationLog();
String msgstr ="";
crossData.put("data_content",crossData.get("data_content").toString());
ComPar comPar = JSONObject.toJavaObject(crossData,ComPar.class);
if(comPar.getDataContent() == null){
msgstr = "数据内容包为空";
log.info(msgstr);
operationLog.setOperationMsg(msgstr);
operationLogDao.insertOperationLog(operationLog);
return "error:"+msgstr;
}else {
//接收到数据后,解析入库保存
int res = comParDao.insertComPar(comPar);
if(res == 1){
return "success";
}else {
msgstr = "数据入库失败";
log.error(msgstr);
operationLog.setOperationMsg(msgstr);
operationLogDao.insertOperationLog(operationLog);
return "error:"+msgstr;
}
}
}
2.接收端代码
controller类:
@RestController
@CrossOrigin
@RequestMapping("/comPar")
public class Controller {
@Autowired
private Service service;
/**
* 接收跨链数据(web接口)
*
* @return
*/
@RequestMapping(value = "/recCrossDataByWeb")
@ResponseBody
public CommonResult recCrossData(@RequestBody Map<String, Object> crossData) {
JSONObject jsonObject = new JSONObject(crossData);
String res = service.RecData(jsonObject);
return "success".equals(res) ?CommonResult.success():CommonResult.failed(res);
}
}
3.postman测试及数据入库结果
postman请求web接口进行数据测试:
?数据入库后:
?
?
|