Search code examples
javaspring-kafka

How to convert GenericData to JsonNode


Stacked in next task: when I consume new Kafka Avro message

@KafkaListener(topics = "#{'${kafka.topic.name}'}",
        properties = {"specific.avro.reader=false"})
public void consume(ConsumerRecord<String, GenericData> record) 

I need to get JsonNode from message value

try {
        JsonNode recordNode = mapper.readTree(record.value().toString());
        } catch (IOException ex) {
            throw new IllegalArgumentException("Failed to parse consumer record " + key, ex);
        }

and here I get java.lang.ClassCastException: org.apache.avro.generic.GenericData$Record cannot be cast to org.apache.avro.generic.GenericData what's wrong here?


Solution

  • As the error suggests, its not the GenericData you get there in ConsumerRecord but the GenericData.Record. Did you try with?

        public void consume(ConsumerRecord<String, GenericData.Record> record)
    

    or

        public void consume(ConsumerRecord<String, GenericRecord> record)