我正在尝试获取列表的结果,基本上是使用Jersey RESTful API(服务器和客户端)的实体列表
UserRESTClient client = new UserRESTClient();
ClientResponse response = client.getUsersByType(ClientResponse.class, String.valueOf(userType));
List<User> participants = response.getEntity(new GenericType<List<User>>() {
});
然而,如果实体用户有一个复合对象,例如,则上述代码不起作用
public class User {
private UserId userId;
}
public class UserId {
private int id;
private int categoryId;
}
在这种情况下,JSON由Jersey反序列化,并为Class User内的字段类型UserId返回null。我检查了返回的JSON,RESTful服务器端的一切似乎都很好,但客户端没有清楚地处理嵌套的JSON响应。
如有任何帮助,我们将不胜感激。我不确定是不是因为杰克逊的预处理器。
以下是实际的代码段。它包括两个类Participant和ParticipantPK(每个Participant的主类)。
@Entity
@Table(name = "conference_participant")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Participant.findAll", query = "SELECT p FROM Participant p"),
public class Participant implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected ParticipantPK participantPK;
}
@Embeddable
public class ParticipantPK implements Serializable {
@Basic(optional = false)
@NotNull
@Column(name = "conference_id")
private int conferenceId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 150)
@Column(name = "participant_sip_uri")
private String participantSipUri;
public ParticipantPK() {
}
public ParticipantPK(int conferenceId, String participantSipUri) {
this.conferenceId = conferenceId;
this.participantSipUri = participantSipUri;
}
以及检索ClientResponse的代码,
List<Participant> participants = response.getEntity(new GenericType<List<Participant>>() {
});
但是,ParticipantPK(复合PK)为空。
您只粘贴了一个代码片段,所以我不知道这部分是否被排除在外,但在我的代码中,我没有字段的setter。我有进攻球员,但没有二传球员。
如果没有setter,我的复合对象本身是非null的,但这些对象的成员本身是null的。
我试图复制它,但使用相同的数据结构对我有效。你使用的是什么版本的Jersey?User类是用@XmlRootElement注释的,还是使用POJO映射功能?