要确保所发送的数据来回是不是在我的RESTful Web服务冗余,每一个嵌套的对象只有它的ID序列化(A Message
的User
创造者不仅具有userId
因为这两个序列客户端和服务器已经知道用户的所有细节)。JSON反序列化从ID为对象
序列化完美地工作,制造该:
{"messageCreatorUser":"60d01602-c04d-4a3f-bbf2-132eb0ebbfc6","messageBody":"Body", ...}
问题:反序列化不产生嵌套对象仅具有其ID。生成的反序列化嵌套对象为null。
下面是前面提到的Message
和User
对象。从此处指定的第三个选项使用序列化“策略”:How to serialize only the ID of a child with Jackson。
Message.java
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "messageId")
public class Message implements Serializable {
// -- Hibernate definitions omitted --
private UUID messageId;
// -----------------------------------------------------------------
// Here is what makes the serializer print out the ID specified on the class definition
@JsonIdentityReference(alwaysAsId = true)
// Here is my attempt to get the User class back when I deserialize
@JsonDeserialize(as = User.class)
@JsonSerialize(as = User.class)
private User messageCreatorUser;
// ------------------------------------------------------------------
// -- more arbitrary properties --
public Message() {
}
public Message(UUID messageId) {
this.messageId = messageId;
}
public Message(String messageId) {
this.messageId = UUID.fromString(messageId);
}
// -- getters and setters --
User.java
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "userId")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User implements Serializable {
private UUID userId;
// -- other arbitrary properties --
public User() {
}
public User(UUID userId) {
this.userId = userId;
}
public User(String userId) {
this.userId = UUID.fromString(userId);
}
// -- getters and setters --
预期反序列化对象:
Message object =
String messageBody = "Body";
User messageCreatorUser =
UUID userId = 60d01602-c04d-4a3f-bbf2-132eb0ebbfc6;
实际反序列化对象:
- Wildfly 10.0.Final:
Message object = String messageBody = "Body"; User messageCreatorUser = null;
就像我说,我希望的嵌套
User
对象与的60d01602-c04d-4a3f-bbf2-132eb0ebbfc6
仅使用ID来创建:
- RESTEasy 3.0.15.Final
- 的RESTEasy杰克逊2提供商3.0.15.Final
- 杰克逊2.6.3(注释,核心,数据绑定等)
为什么结果不同?
我会澄清我使用的注释。我有'@JsonIdentityReference(alwaysAsId = true)'以及'@Json(De)Serialize(as = User.class)',希望它创建bean。 –
强制序列化作为一个ID绝对要求所引用的对象在JSON内返回?也许我可以让序列化程序使用ID创建空的'User'对象并将其附加在一起? –
它不是强制的,所以你绝对只能使用id,不需要占位符Object。但是,如果要读回数据,你必须能够以某种方式获取数据 - 这可能是好的,只是想提一下,因为它可能对某些用例有问题。 – StaxMan