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

时间:2018-07-08 10:30:38

标签: java spring rest spring-mvc

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

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

1 个答案:

答案 0 :(得分:3)

这里我假设您要在一个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) {
    ...
}