Ranganath Tirumala
Ranganath Tirumala

Reputation: 103

Kafka Deserialize Nested Generic Types

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.

Upvotes: 5

Views: 1525

Answers (1)

Ranganath Tirumala
Ranganath Tirumala

Reputation: 103

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
}

Upvotes: 5

Related Questions