Java JAX-RS中POST请求中对象内部的映射集合

Java JAX-RS中POST请求中对象内部的映射集合,java,hibernate,jax-rs,Java,Hibernate,Jax Rs,我有一门课叫Customer @Entity @Table(name = "customer") public class Customer { @Id @Column(unique = true) private String userId; @Column(unique = true) private String userName; private String fullName; @Column(unique = true)

我有一门课叫Customer

@Entity
@Table(name = "customer")
public class Customer {
    @Id
    @Column(unique = true)
    private String userId;
    @Column(unique = true)
    private String userName;
    private String fullName;
    @Column(unique = true)
    private String emailAddress;
    private String password;
    private String country;
    @ElementCollection
    private Collection<ContactNum> contactNums =  new ArrayList<>();
    private String district;
    private String dateOfBirth;
    private String gender;
}
我的RESTAPI得到一个POST请求JSON对象,该对象是客户和其中的联系人号码

{
  "userName": "aaaa",
  "fullName": "aaaa",
  "emailAddress": "aaaa",
  "password": "aaaa",
  "country": "aaaa",
  "contactNums" : {
    "landLine": "0000000000",
    "mobile": "0000000000"
  },
  "district": "aaaa",
  "dateOfBirth": "813695400000",
  "gender": "aaaa"
}
如何在JAX-RS客户机中映射该请求?我获取请求的方法如下。我还使用Hibernate作为ORM工具

 @POST
 @Path("registerCustomer")
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 public Response registerCustomer(Customer newCustomer) {
 }

如果您使用Jackson处理JSON转换

  • 您可以使用自定义反序列化器(通过 在班级层面)
  • 或者编写一个转换ContactNum的适配器 到ContactNum的集合
但是如果您将JSON输入从

"contactNums" : {
"landLine": "0000000000",
"mobile": "0000000000"
}

(contactNums从对象更改为对象数组)


这样,转换应该是开箱即用的

@HarshitShrivastava这不是一个Spring应用程序,你使用哪种JAX-RS实现?@TomVW我使用Jsersey“Jersey使用Jackson处理JSON转换。”它可能使用,但这不是默认的JSON提供程序。我想com.sun.Jersey:Jersey JSON使用Jackson作为默认提供程序。作为JSON绑定的默认提供程序。我不知道,我从答案中删除了它
"contactNums" : {
"landLine": "0000000000",
"mobile": "0000000000"
}
"contactNums" : [{
"landLine": "0000000000",
"mobile": "0000000000"
}]