3

Is it possible to pass multiple objects in @RequestBody?

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

Marina
  • 58
  • 1
  • 7
  • Maybe duplication. https://stackoverflow.com/questions/50430855/how-to-pass-2-objects-in-requestbody – Pasha Jul 08 '18 at 10:32
  • Please provide more details about the questions. What you want is two objects of @RequestBody. If so how does your request look like – vizsatiz Jul 08 '18 at 10:33

1 Answers1

5

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) {
    ...
}
Alien
  • 15,141
  • 6
  • 37
  • 57