问题
Given a class like this
public class Message<T> implements Serializable {
final String correlationId;
final LocalDateTime timestamp;
final T payload
}
How to implement a custom Kafka deserializer that can handle the nested generic type?
Serialization should be pretty straight forward, as the type information will be available.
But how to handle not having the type information when deserialising?
p.s: I am using jackson to do the serialization / deserialization.
回答1:
solved by getting jackson to include the type info when serializing.
public class Message<T> implements Serializable {
final String correlationId;
final LocalDateTime timestamp;
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
final T payload
}
来源:https://stackoverflow.com/questions/51834248/kafka-deserialize-nested-generic-types