Reputation: 63
I'm trying to find a way kafka consumer to be able to read(deserialize) different json structure messages from the topic. To build some kind of abstraction may be with class id. If someone has done it I will be happy to advise.
Cheers :)
Upvotes: 2
Views: 1999
Reputation: 174759
See the Spring for Apache Kafka documentation.
The JsonDeserializer
can use type information in headers to determine which type to create during deserialization.
If the source application is also Spring, the serializer will automatically add this type information for you.
If not, you can add it a header yourself in a header named __TypeId__
(by default).
The value can either be a fully qualified class name, or a token value, with the deserializer configured to map that value to a class name. Again, see the documentation.
If you can't set a header and need to examine the JSON to determine the type, you could start with that deserializer and make a custom version.
Upvotes: 2