Kafka反序列化嵌套泛型类型 [英] Kafka Deserialize Nested Generic Types

查看:485
本文介绍了Kafka反序列化嵌套泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出这样的课程

public class Message<T> implements Serializable {
  final String correlationId;
  final LocalDateTime timestamp;
  final T payload
}

如何实现可处理嵌套通用类型的自定义Kafka解串器?

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:我正在使用jackson进行序列化/反序列化.

p.s: I am using jackson to do the serialization / deserialization.

推荐答案

通过在序列化时让jackson包含类型信息来解决.

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
}

这篇关于Kafka反序列化嵌套泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆