天天看點

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