Marina
Marina

Reputation: 58

Is it possible to pass multiple objects in @RequestBody?

Is it possible to pass multiple objects in @RequestBody?

I need it in order to get rid of bicycles inventions and creating utility classes.

Upvotes: 3

Views: 9716

Answers (1)

Alien
Alien

Reputation: 15878

Here i am assuming that you want to send two class objects in a single responsebody response.

Create an additional inner class in your Controller that resembles both the entities

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) {
    ...
}

Upvotes: 5

Related Questions