2

I use Jersey to provide REST API. I wrote controller, which map request to entity:

@POST
@Path("register")
@Consumes("application/json")
@Produces("application/json")
public Response UserRegistration(User UserData)
{


}

It works fine for simple json like:

{
"email":"email@email.pl",
"name":"Name",
"surname":"surname"
}

and entity:

    public class User {

    public User() {}

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id",unique = true)
    private int id;
    @Column(name = "name")
    private String name;
    @Column(name = "surname")
    private String surname;
    @Column(name = "email", unique = true)
    private String email;

    //getters

    // setters
}

How can I map JSON array like:

{
"email":"email@email.pl",
"name":"Name",
"surname":"surname"
"reqData":{
    "ip":44.44.44.44}
}

To two entites? First entity: user (shown above) and entity reqData:

public class RegistrationData {

    public RegistrationData(){};

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true)
    private int id;
    @Column(name = "ip")
    private int ip;

I have of course One to One relationship, but it is not important here(I think). I hope my question is understandable. Thanks for help.

4

1 回答 1

0

我做的!

我不得不修改我的 json 请求并制作额外的课程。

它对我来说效果很好。我认为框架会为我做这件事:<

新的 JSON:

{
   "user":{
      "email":"email@email.com",
      "name":"xxxx",
      "surname":"fffff"
   },
   "reqData":{
      "ip":"1223123"
   }
}

映射:(将返回“email@email.com”)

    @POST
    @Path("test")
    @Consumes("application/json")
    @Produces("application/json")
    public String Test(Test Testclass)
    {
        return Testclass.getUser().getEmail();
    }
}

和班级

public class Testclass {

    public Testclass(){};

    // getters 

    //setters
    private User user;

    private RegistrationData reqData;
}
于 2014-06-13T15:24:50.070 回答