1. 传入参数格式
{
"contractId": 2611141940251005036,
"documentParams": [
{
"name": "乙方姓名2",
"value": "邓茜茜"
},
{
"name": "乙方姓名3",
"value": "邓茜"
}
]
}
2. 后端接收和解析方式
- 使用JSONObject:
@PostMapping("/contract/fillParams")
public Result<String> fillParams(@RequestBody JSONObject object){
Result<String> result = new Result<>();
String contractId = object.getString("contractId");
String bizId = object.getString("bizId");
JSONArray documentParams = object.getJSONArray("documentParams");
List<DocumentParamDto> documentParamDtos = documentParams.toJavaList(DocumentParamDto.class);
try {
result = contractLockService.fillParams(Long.valueOf(contractId),bizId,documentParamDtos);
}catch (Exception e) {
e.printStackTrace();
result.error500(e.getMessage());
}
return result;
}
对于list对象,使用了aibaba的getJSONArray接收后,再toJavaList解析;
- 使用map:
@PostMapping("/contract/fillParams")
public Result<String> fillParams(@RequestBody Map<String,Object> map){
Result<String> result = new Result<>();
String contractId = map.get("contractId").toString();
Object documentParams = map.get("documentParams");
List<DocumentParamDto> documentParamDtos = JSON.parseArray(JSON.toJSONString(documentParams), DocumentParamDto.class);
return result;
}
对于list对象,这里用Object 接收,然后是对Object 转为JSONString,然后再转为List
- 使用hutool工具:
JSONObject object = JSONObject.parseObject(execute);
String result1 = object.getString("result");
if (ResponseCodeEnum.SUCCESS.getCode()
.equals(Integer.parseInt(object.getString("code")))){
result.setCode(200);
JSONArray jsonArray = JSONUtil.parseArray(result1);
result.setResult(JSONUtil.toList(jsonArray, TemplateEntity.class));
result.setMessage(object.getString("message"));
return result;
}
这里是用String接收,然后转JSONArray ,再转为List;
- 正常情况下是@RequestBody +实体来接收,比如:
@PostMapping("/template/insert")
public Result<String> insertTemplateList(@RequestBody TemplateEntity templateEntity){
Result<String> result = new Result<>();
if (NullUtil.isEmpty(templateEntity)){
result.error500("传入参数为空!");
return result;
}
try {
contractLockService.insertTemplateList(templateEntity);
}catch (Exception e) {
e.printStackTrace();
result.error500("插入异常!");
return result;
}
return null;
}
补充:
对于form-data的类型:
后端有两种接收方式:
- @RequestParam的方式
@PostMapping("/contract/send")
public Result<String> sendContract(@RequestParam("contractId")String contractId,
@RequestParam("bizId")String bizId){
Result<String> result = new Result<>();
try {
result = contractLockService.sendContract(Long.valueOf(contractId),bizId);
}catch (Exception e) {
e.printStackTrace();
result.error500(e.getMessage());
}
return result;
}
- 直接使用实体
@PostMapping("/contract/send")
public Result<String> sendContract(ContractDto contractDto){
Result<String> result = new Result<>();
try {
result = contractLockService.sendContract(Long.valueOf(contractId),bizId);
}catch (Exception e) {
e.printStackTrace();
result.error500(e.getMessage());
}
return result;
}
因为form-data的方式就是键值对的方式;
另外application/x-www-form-urlencoded的格式也是直接使用实体:
@PostMapping("/received")
public Result<String> signatureCallback(SignatureInfoDto signatureInfoDto){
Result<String> result = new Result<>();
if (NullUtil.isEmpty(signatureInfoDto)){
result.setCode(1000001);
result.setMessage("传入参数为空!");
result.setSuccess(false);
return result;
}
return result;
}