zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    html+css 笔记
    JS随手笔记
    JQ几个淡入淡效果
    AngularJS编译阶段应分为两个阶段
    JavaScript 原型链的理解
    js继承的6种方式
    什么是跨域?跨域解决方法
    computed (计算属性) 和 methods (方法) 的区别
    谈谈vue生命周期
    基本类型有哪几种?null 是对象吗?基本数据类型和复杂数据类型存储有什么区别?
  • 原文地址:https://www.cnblogs.com/wl1202/p/10830890.html
Copyright © 2011-2022 走看看