Spring MVC 接收多个实体参数

在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个属性就都有赋值

原文地址:https://www.cnblogs.com/dwb91/p/7347290.html