Kafka Deserialize Nested Generic Types

☆樱花仙子☆ 提交于 2019-12-23 18:33:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!