//界面
public interface NotificationPayload {
}
//类实现接口(interface)
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) 将其再次转换为通知
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但似乎它没有帮助,因为我有太多的类从接口(interface)实现,而不仅仅是一次。
最佳答案
需要使用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
关于java - jackson 抛出 JsonMappingException 无法构造实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52181308/