gpt4 book ai didi

java - 从 ID 到对象的 JSON 反序列化

转载 作者:行者123 更新时间:2023-12-02 11:59:20 24 4
gpt4 key购买 nike

为了确保在我的 RESTful Web 服务中来回发送的数据不是多余的,每个嵌套对象仅对其 ID 进行序列化(MessageUser 创建者只有 userId 序列化,因为客户端和服务器都已经知道用户的所有详细信息)。

序列化工作完美,产生这样的结果:

{"messageCreatorUser":"60d01602-c04d-4a3f-bbf2-132eb0ebbfc6","messageBody":"Body", ...}

问题:反序列化不会生成仅具有 ID 的嵌套对象。生成的反序列化嵌套对象为 null。

这里是前面提到的 MessageUser 对象。序列化“策略”是从此处指定的第三个选项中使用的: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;

实际反序列化对象:

Message object =
String messageBody = "Body";
User messageCreatorUser = null;

就像我说的,我希望仅使用 ID 为 60d01602-c04d-4a3f-bbf2-132eb0ebbfc6 创建一个嵌套的 User 对象

使用:

  • Wildfly 10.0.Final:
    • RESTEasy 3.0.15.Final
    • RESTEasy Jackson 2 提供商 3.0.15.Final
      • Jackson 2.6.3(注释、核心、数据绑定(bind)等)

为什么结果不同?

最佳答案

正如这里的答案( Jackson deserialize JsonIdentityReference (alwaysAsId = true) )所述,使用 setter 反序列化器效果很好,并且不需要令人讨厌的自定义反序列化器:

Message.java

    @JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "messageId")
public class Message implements Serializable {

private UUID messageId;

@JsonIdentityReference(alwaysAsId = true)
private User messageCreatorUser;

// -- other fields and such --

public User getMessageCreatorUser() {
return messageCreatorUser;
}

public void setMessageCreatorUser(User messageCreatorUser) {
this.messageCreatorUser = messageCreatorUser;
}

@JsonProperty("messageCreatorUser")
public void setMessageCreatorUser(String userId) {
this.messageCreatorUser = new User(userId);
}

User.java

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "userId")
public class User implements Serializable {

private UUID userId;

// -- other fields --

public User(String userId) {
this.userId = UUID.fromString(userId);
}

}

请注意,为了使用 @JsonIdentityReference(alwaysAsId = true),您需要在某处拥有 @JsonIdentityInfo(...)

关于java - 从 ID 到对象的 JSON 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38443016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com