1. 程式人生 > >springCloud發送請求多對象參數傳遞問題

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

bject 方法 int 代碼 ssi put upd 題解 attr

今天做修改的時候遇到個很奇怪的問題,參數是兩個對象,直接放到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.

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