如何在不创建自定义DTO的情况下解析同一JSON请求/响应中的多个对象
我有两个模型课如何在不创建自定义DTO的情况下解析同一JSON请求/响应中的多个对象,json,spring-mvc,jersey,Json,Spring Mvc,Jersey,我有两个模型课 Class User { } Class UserProfile { } 我想使用SpringMVC和JSON在同一请求/响应中发送/接收(@GET,@POST)多个对象 例如: { "userprofile" : { "id":1, name:"test1" }, "user" : {"id": 161, "name": "x"} } 确保它位于类路径上。在控制器中创建一个类似于 static class UserAndPro
Class User
{
}
Class UserProfile
{
}
我想使用SpringMVC和JSON在同一请求/响应中发送/接收(@GET,@POST)多个对象
例如:
{
"userprofile" : { "id":1, name:"test1" },
"user" : {"id": 161, "name": "x"}
}
确保它位于类路径上。在控制器中创建一个类似于
static class UserAndProfile {
public UserProfile userprofile;
public User user;
}
然后您的请求映射将类似于
@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) {
...
}
有关更多详细信息,请参阅。确保位于类路径上。在控制器中创建一个类似于
static class UserAndProfile {
public UserProfile userprofile;
public User user;
}
然后您的请求映射将类似于
@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) {
...
}
有关更多详细信息,请参阅。好的,您想这样做。到现在为止你都做了些什么?好吧,你想这么做。到现在为止你做了什么?@Francis,我真的需要为此创建一个类吗?没有它就不可能吗?@Francis,我真的需要为此创建一个类吗?没有这个就不可能吗?