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

时间:2018-09-05 09:09:31

标签: java json jackson jackson-databind

//界面

public interface NotificationPayload {
}

//类实现接口

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实现,而不仅仅是一次。

1 个答案:

答案 0 :(得分:0)

您需要使用杰克逊的注释来实现多态反序列化。

@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