杰克逊抛出 JsonMappingException 无法构造实例

jackson throw JsonMappingException can not construct instance of

//界面

public interface NotificationPayload {
}

//Classes 实现接口

public ClassA implements NotificationPayload {...}
public ClassB implements NotificationPayload {...}
...
public ClassX implements NotificationPayload {...}

//要发送的消息

public class Notification<T extends NotificationPayload> {
    private T data; //T may be ClassA/ClassB/ClassC.../ClassX
    ...
}

当我收到消息 json 时,我想再次使用 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)

将其转换为 Notification

json 示例:

{
    "type":"ClassA",
    "userId":10087
}

我转换它:

Notification notif = objectMapper.readValue(json, Notification.class);

它抛出异常:

java.lang.IllegalArgumentException: Can not construct instance of 
com.common.kafka.notification.NotificationPayload: abstract types
either need to be mapped to concrete types, have custom deserializer, or
contain additional type information
at [Source: N/A; line: -1, column: -1] (through reference chain:
com.common.kafka.notification.Notification["data"])

我读过这个问题:Cannot construct instance of - Jackson 但似乎它无济于事,因为我有太多 Class 从界面实现,而不仅仅是一次。

需要使用jackson的注解来实现多态反序列化

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = ClassA.class, name = "ClassA"),
        @JsonSubTypes.Type(value = ClassB.class, name = "ClassB")
})
public interface NotificationPayload {
}

public class ClassA implements NotificationPayload {

    private Integer userId;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }
}

public class Notification <T extends NotificationPayload> {

    private T data; //T may be ClassA/ClassB/ClassC.../ClassX

    @JsonCreator
    public Notification(T data) {
        this.data = data;
    }

    public static void main(String[] args) throws IOException {

        String jsonStr = "{\"type\":\"ClassB\",\"userId\":10087}";

        ObjectMapper objectMapper = new ObjectMapper();
        Notification notification = objectMapper.readValue(jsonStr, Notification.class);
    }
}

你能找到的所有注释here