在SpringMVC 的接收参数中,如果接收一个实体对象,只需要在方法参数中这样做:@RequestBody User user

//单个的时候这样接收 
@RequestMapping(value = "/user/default/save",method = RequestMethod.POST)
    public ResponseBasicBean saveUserAndMore(@RequestBody User  user) {
        return null;
    }

 //批量的时候这样接收
    @RequestMapping(value = "/user/job/save",method = RequestMethod.POST)
    public ResponseBasicBean saveUserJob(@RequestBody List<UserJobHistory> jobs) {
        return userBusinessService.saveUserJob(jobs);
    }

但是如果是接收2个不同的对象,怎么做呢?如果直接 写2个@RequestBody User user,@RequestBody Person person

这样会报错400;而解决办法是 新建一个类Human,里面有2个属性,分别是user和person,以及getter setter方法;

然后前端提交json数据的时候,需要加上一个嵌套{"user":{"id":1,"name":"ding"},"person":{"id":2,"sex":2}}

最后在后台方法参数里面直接用 @RequestBody Human human ,这个human对象里面的2个属性就都有赋值

相关文章:

  • 2022-01-26
  • 2021-07-04
  • 2022-12-23
  • 2021-10-14
  • 2021-09-14
  • 2021-09-29
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-09
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案