I\'m trying to deserialize different JSON payloads from the same Kafka topic. The other questions asked here, guided me to a first attempt, but I was not able to get it runn
You can't do that; you have 2 different listener containers with listeners that expect different objects.
For multiple listener methods that receive different types, you need to use @KafkaListener
at the class level and @KafkaHandler
at the method level.
See @KafkaListener on a Class.
When using @KafkaListener at the class-level, you specify @KafkaHandler at the method level. When messages are delivered, the converted message payload type is used to determine which method to call.
@KafkaListener(id = "multi", topics = "myTopic")
static class MultiListenerBean {
@KafkaHandler
public void listen(String foo) {
...
}
@KafkaHandler
public void listen(Integer bar) {
...
}
@KafkaHandler(isDefault = true`)
public void listenDefault(Object object) {
...
}
}
The default method is optional and is used for unknown payload types.
But this only works with a smart deserializer (that knows how to convert to different payloads).
Or, you can add a RecordFilterStrategy
to the listener container factory to skip the other records in each listener.