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

查看:58
本文介绍了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?

ps:我正在使用 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天全站免登陆