Search code examples
jsonspring-bootapache-kafkaspring-kafkajson-deserialization

Spring Kafka listener to consume JSON and infer the domain object


I have a topic that will be published with multiple types of JSON messages. I don't have control over the publisher code to add any headers etc. But, I want to leverage @KafkaHandler to handle the different JSON messages inferred to the domain objects. I got some references https://github.com/spring-projects/spring-kafka/tree/master/samples/sample-02

As I don't have control over the publisher code, with the custom deserializer I want to handle multiple JSON types. Any references to write custom de-serializer to handle multiple JSON objects with @KafkaHandler.


Solution

  • You can't use class level listeners without deserialization. It's a catch-22.

    To determine which method to call, we need to know the type (after deserialization); we can't infer the type if it hasn't been deserialized yet.

    You could write a wrapper for muliple JSON deserializers, either using try...until success, or by "peeking" at the JSON to determine hueristically which deserializer to call for this record.

    if (json.contains "\"foo\":") {
         return deserializeWithFooDeser(data, headers);
    }