springCloud發送請求多對象參數傳遞問題


今天做修改的時候遇到個很奇怪的問題,參數是兩個對象,直接放到map中向消費者傳遞,方法用map接收,死活接收不到,問了下前輩說map中是多對象時接收容易出錯,推薦我傳遞JSON,照他說的把問題解決了,代碼發上來以后長個記性。

先看有問題的,請求:

public R updateChargeStandard(CaChargeStandardTemplate caChargeStandardTemplate){
        SysEmployeeInfo user = (SysEmployeeInfo) SecurityUtils.getSubject().getSession().getAttribute("user");
        Map<String,Object> params = new HashMap<>();
        params.put("user",user);
        params.put("caChargeStandardTemplate",caChargeStandardTemplate);
       return chargeStandardService.updateChargeStandard(params);
    }

接收:

  public R updateChargeStandard(@RequestBody Map<String,Object> params){
        CaChargeStandardTemplate caChargeStandardTemplate = (CaChargeStandardTemplate)params.get("caChargeStandardTemplate");
        SysEmployeeInfo user = (SysEmployeeInfo)params.get("user");
        int i = chargeStandardService.updateChargeStandard(caChargeStandardTemplate,user);
        return  i > 0 ? R.ok("保存成功"):R.error("保存失敗");
    }

再看修改之后的

請求:

 public R updateChargeStandard(CaChargeStandardTemplate caChargeStandardTemplate){
        SysEmployeeInfo user = (SysEmployeeInfo) SecurityUtils.getSubject().getSession().getAttribute("user");
        Map<String,Object> params = new HashMap<>();
        String userStr = JSON.toJSONString(user); //對象轉String再放進map中
        String caChargeStandardTemplateStr = JSON.toJSONString(caChargeStandardTemplate);
        params.put("user",userStr);
        params.put("caChargeStandardTemplate",caChargeStandardTemplateStr);
       return chargeStandardService.updateChargeStandard(params);
    }

接收:

public R updateChargeStandard(@RequestBody Map<String,Object> params){
        String caChargeStandardTemplateStr = (String) params.get("caChargeStandardTemplate");//從map中取String
        CaChargeStandardTemplate caChargeStandardTemplate = JSON.parseObject(caChargeStandardTemplateStr,CaChargeStandardTemplate.class);//String轉對象
        String userStr = (String) params.get("user");//從map中取String
        SysEmployeeInfo user = JSON.parseObject(userStr,SysEmployeeInfo.class);//String轉對象
        int i = chargeStandardService.updateChargeStandard(caChargeStandardTemplate,user);
        return  i > 0 ? R.ok("保存成功"):R.error("保存失敗");
    }

end.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM