Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不创建自定义DTO的情况下解析同一JSON请求/响应中的多个对象_Json_Spring Mvc_Jersey - Fatal编程技术网

如何在不创建自定义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,我真的需要为此创建一个类吗?没有这个就不可能吗?