最近开发一款软件,遇到个问题,需要创建个方法接受post的请求。
后台写好了:
/**
* 将OSS报警导入钉钉机器人
* @param checksum
* @param content
* @return
*/
@RequestMapping(value = "/receive", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
@ResponseBody
public ResultData<Boolean> receiveWarning(@RequestParam String checksum, @RequestParam String content) {
try {
log.info("receiveWarning 入参,content:{},checksum:{}", content,checksum);
OssWarning ossWarning = JSON.parseObject(content, OssWarning.class);
if(ossWarning==null ||ossWarning.getBucket()==null|| ossWarning.getAuditResult()==null){
return ResultData.error(PmsErrorEnum.PARAM_VALID_ERROR);
}
String bucket=ossWarning.getBucket();
AuditResult auditResult=ossWarning.getAuditResult();
if(!MAIZUO_BUCKET.equals(bucket)){
return ResultData.error(PmsErrorEnum.PARAM_VALID_ERROR);
}
if(AUDIT_SUCC.equals(auditResult.getResoureStatus())){
return ResultData.error(PmsErrorEnum.PARAM_VALID_ERROR);
}
log.info("receiveWarning 过滤之后的入参,ossWarning:{}", JSON.toJSONString(content));
sendWarningUtil.sendCallBackError(ossWarning,content);
return ResultData.success(true);
} catch (MzException me) {
log.warn("SendWarningController.receiveWarning-->OSS处理图片发生业务异常,content:{},me:{}", content, me);
return ResultData.error(me);
} catch (Exception e) {
log.warn("SendWarningController.receiveWarning-->字符串格式异常,content:{},e:{}", content, e);
return ResultData.error(SystemErrorEnum.SYSTEM_ERROR);
}
}
问题很简单:post请求的参数 @Request String checksum 和@Request String content,

然后自己在postman里面模拟了post请求:
下面是我模拟的请求:
后来发现个问题:只要第二个参数:里面有list,模拟请求就会失败;
这是可以运行的参数:主要是第二个参数content,第一个checksum没问题
可以运行的:
{"bucket":"maizuo-saas","freezed":false,"object":"xxx.jpg","region":"cn-hangzhou","stock":false}
不可以运行的:
{"results":[{"label":"porn","rate":100,"scene":"porn","suggestion":"block"},{"label":"normal","rate":99.6,"scene":"terrorism","suggestion":"pass"}]}
我试了一下 只要参数里面带有list ,postman就没有任何返回,也不提示报错信息,只要去掉list,就恢复正常。
如下可以运行的postMan:
加上list之后不能运行的postman:
由于没有任何报错信息,没法修改程序,
后来想到了使用curl进行模拟,看看能不能有返回信息:
这是curl:
curl https://pms-pre.maitix.com/pms/warning/receive -X POST -d '{"checksum": "kanzeoss","content":"{"bucket":"maizuo-saas","freezed":false,"object":"xxx.jpg","region":"cn-hangzhou","ScanResult":{"code":200,"dataId":"5C919E2FBD6CD6940D4A8B46","msg":"OK","results":[{"label":"porn","rate":100,"scene":"porn","suggestion":"block"},{"label":"normal","rate":99.6,"scene":"terrorism","suggestion":"pass"}],"taskId":"img1ZxzVQUuAz36scZEjyGXzx-1qpzhY"},"auditResult":{"resoureStatus":1,"suggestion":"failed"},"stock":false}"}' --header "Content-Type: application/json"
结果立即出来了:
{"timestamp":1605600704113,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required String parameter 'checksum' is not present","path":"/pms/warning/receive"}
报错信息里面有:
"message":"Required String parameter 'checksum' is not present"
根据这个查询百度:org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'xxx' is not present 报错解决
找到了问题原因:
原因是:我们使用postMan的时候,选择有误:应该使
(1)使用Body;
(2)使用form-data;就可以了
写的程序没有任何问题;只是postMan模拟方式不对而已