1. 程式人生 > 實用技巧 >postman 傳遞json的引數裡面帶了List物件

postman 傳遞json的引數裡面帶了List物件

最近開發一款軟體,遇到個問題,需要建立個方法接受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模擬方式不對而已