4

When using Integer as a key this is not a problem, kafka should be able to handle Strings as a key.

ProducerFactory<String, String> pf =
                new DefaultKafkaProducerFactory<String, String>(senderProps);
KafkaTemplate<String, String> template = new KafkaTemplate<>(pf);
ProducerRecord<String,String> pr = new ProducerRecord<>("my-topic", "key1","test");
        template.send(pr);`

It throws below exception:

Org.apache.kafka.common.errors.SerializationException: Can't convert key of class java.lang.String to class org.apache.kafka.common.serialization.IntegerSerializer specified in key.serializer

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at org.apache.kafka.common.serialization.IntegerSerializer.serialize(IntegerSerializer.java:21) at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:65) at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:55) at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:799) at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:784) at org.springframework.kafka.core.DefaultKafkaProducerFactory$CloseSafeProducer.send(DefaultKafkaProducerFactory.java:285) at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:357) at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:206)

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
kambo
  • 129
  • 2
  • 11

1 Answers1

6

Seems, your Key serializer is set as Integer in ProducerConfig. You need to set the KEY_SERIALIZER_CLASS_CONFIG as String :

@Bean
    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return props;
    }
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
  • Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well ! – kambo Jan 03 '19 at 09:43
  • 1
    Yeah, You need to add in Producer and consumer both whatever key and value type you are using :) If it helps you, please feel free to accept/upvote the answer :) – Nishu Tayal Jan 03 '19 at 09:49
  • 1
    Please don't forget to accept the answer which solves your problem. It will help others :) – Nishu Tayal Jan 03 '19 at 12:18