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. 回答1: Here i am assuming t...

问题:

Is it possible to pass multiple objects in @RequestBody?

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


回答1:

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) {
    ...
}
  • 发表于 2018-07-10 01:51
  • 阅读 ( 153 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除