Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kafka Deserialize Nested Generic Types

Tags:

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.

like image 590
Ranganath Tirumala Avatar asked Aug 14 '18 05:08

Ranganath Tirumala


1 Answers

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
}
like image 130
Ranganath Tirumala Avatar answered Oct 04 '22 17:10

Ranganath Tirumala