是否可以在@RequestBody中传递多个对象?

我需要它来摆脱自行车的发明并创建实用程序类。

最佳答案

在这里,我假设您要在一个responsebody响应中发送两个类对象。

在Controller中创建一个类似于两个实体的附加内部类

static class UserAndProfile {
    public UserProfile userprofile;
    public User user;
}
and then your request mappings would resemble

@RequestMapping(value = "/user", method = RequestMethod.GET)
public @ResponseBody UserAndProfile user()  {
    UserAndProfile userAndProfile = new UserAndProfile();
    userAndProfile.userprofile = ...
    userAndProfile.user = ...
    return userAndProfile;
}

@RequestMapping(value = "/user", method = RequestMethod.POST)
public Object user(@RequestBody UserAndProfile userAndProfile) {
    ...
}

09-11 19:28