如何使用Jackson定义2级继承结构

huangapple go评论117阅读模式
英文:

How to define 2-level inheritance structure with Jackson

问题

我有以下基本(接口)结构

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        property = "messageType",
        visible = true)
@JsonSubTypes({
        @JsonSubTypes.Type(value = AppMessage.class, name = "APP"),   
        @JsonSubTypes.Type(value = NotificationMessage.class, name = "NOTIFICATION"),
})
public interface Message {
    MessageType getMessageType();

    Date getTimestamp();
}

AppMessage 类是一个简单的 POJO(目前如此),类似于

public class AppMessage implements Message {

    private String appId; 
    ...
    private Date timestamp = Date.from(Instant.now());

}

NotificationMessage 是另一个接口

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        property = "NotificationType",
        visible = true)
@JsonSubTypes({
        @JsonSubTypes.Type(value = NotificationAckMessage.class, name = "ACK"),
        @JsonSubTypes.Type(value = NotificationReqMessage.class, name = "REQ"),
})
public interface NotificationMessage extends Message {

    String getDest();
    String getMessage();
    MessageType getMessageType();
    NotificationType getNotificationType();
}

当我想反序列化 NotificationMessage 时,像这样

{"NotificationType": "REQ", "dest": "some dest", "message": "some message", "messageType": "NOTIFICATION", "notificationType": "REQ", "timestamp": 1584299876847}

ObjectMapper objectMapper = new ObjectMapper();
Message msg = objectMapper.readValue(msgStr, Message.class);

我得到

无法构造 NotificationMessage 的实例:抽象类型需要映射到具体类型,具有自定义反序列化程序,或包含附加类型信息

当然,AppMessage 在不出现任何错误的情况下解析。

如何在不展开它的情况下实现这种结构和逻辑,即在 Message 注解级别定义所有子类型?

谢谢!

英文:

I have the following base (interface) structure

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        property = "messageType",
        visible = true)
@JsonSubTypes({
        @JsonSubTypes.Type(value = AppMessage.class, name = "APP"),   
        @JsonSubTypes.Type(value = NotificationMessage.class, name = "NOTIFICATION"),
})
public interface Message {
    MessageType getMessageType();

    Date getTimestamp();
}

the AppMessage class is a simple POJO (for now) like

public class AppMessage implements Message {

    private String appId; 
    ...
    private Date timestamp = Date.from(Instant.now());

}

but the NotificationMessage is another interface

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        property = "NotificationType",
        visible = true)
@JsonSubTypes({
        @JsonSubTypes.Type(value = NotificationAckMessage.class, name = "ACK"),
        @JsonSubTypes.Type(value = NotificationReqMessage.class, name = "REQ"),
})
public interface NotificationMessage extends Message {

    String getDest();
    String getMessage();
    MessageType getMessageType();
    NotificationType getNotificationType();
}

and of course two more pojos as NotificationAckMessage and NotificationReqMessage classes which implements NotificationMessage.

When ever I want to deserialize NotificationMessage like

{"NotificationType": "REQ", "dest": "some dest", "message": "some message", "messageType": "NOTIFICATION", "notificationType": "REQ", "timestamp": 1584299876847}

ObjectMapper objectMapper = new ObjectMapper();
Message msg = objectMapper.readValue(msgStr, Message.class);

I get
> Can not construct instance of NotificationMessage: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

of course the AppMessage getting parsed without any errors.

How can I achieve this kind of structure and logic without flatten it, i.e define all the subtypes at the Message annotation level?

Thanks!

答案1

得分: 4

这似乎是不受支持的。看一下这个问题:https://github.com/FasterXML/jackson-databind/issues/374

最后的回答:
> 这不受支持,也没有支持的计划。不要设计你的系统,假设这将会被实现。

英文:

It seems that this is not supported. Look at this issue: https://github.com/FasterXML/jackson-databind/issues/374

The last answer:
> It is not supported and there are no plans to support it. Do not design your system assuming this will be implemented.

huangapple
  • 本文由 发表于 2020年3月16日 04:11:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/60697181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定