今天做修改的时候遇到个很奇怪的问题,参数是两个对象,直接放到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.