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.

转载于:https://www.cnblogs.com/wl1202/p/10830890.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值