Sending using KafkaTemplate returns a exception for using String as a key





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















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)











share|improve this question































    1















    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)











    share|improve this question



























      1












      1








      1








      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)











      share|improve this question
















      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)








      spring apache-kafka spring-kafka spring-kafka-test






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 9:20









      Nishu Tayal

      12.9k73584




      12.9k73584










      asked Jan 3 at 9:12









      kambokambo

      104




      104
























          1 Answer
          1






          active

          oldest

          votes


















          2














          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;
          }





          share|improve this answer
























          • Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !

            – kambo
            Jan 3 at 9: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 3 at 9:49






          • 1





            Please don't forget to accept the answer which solves your problem. It will help others :)

            – Nishu Tayal
            Jan 3 at 12:18











          • +1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers

            – Artem Bilan
            Jan 3 at 14:40











          • thank you, didn't know about that :)

            – kambo
            Jan 4 at 21:45












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019235%2fsending-using-kafkatemplatestring-string-returns-a-exception-for-using-string%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          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;
          }





          share|improve this answer
























          • Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !

            – kambo
            Jan 3 at 9: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 3 at 9:49






          • 1





            Please don't forget to accept the answer which solves your problem. It will help others :)

            – Nishu Tayal
            Jan 3 at 12:18











          • +1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers

            – Artem Bilan
            Jan 3 at 14:40











          • thank you, didn't know about that :)

            – kambo
            Jan 4 at 21:45
















          2














          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;
          }





          share|improve this answer
























          • Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !

            – kambo
            Jan 3 at 9: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 3 at 9:49






          • 1





            Please don't forget to accept the answer which solves your problem. It will help others :)

            – Nishu Tayal
            Jan 3 at 12:18











          • +1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers

            – Artem Bilan
            Jan 3 at 14:40











          • thank you, didn't know about that :)

            – kambo
            Jan 4 at 21:45














          2












          2








          2







          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;
          }





          share|improve this answer













          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;
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 9:23









          Nishu TayalNishu Tayal

          12.9k73584




          12.9k73584













          • Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !

            – kambo
            Jan 3 at 9: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 3 at 9:49






          • 1





            Please don't forget to accept the answer which solves your problem. It will help others :)

            – Nishu Tayal
            Jan 3 at 12:18











          • +1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers

            – Artem Bilan
            Jan 3 at 14:40











          • thank you, didn't know about that :)

            – kambo
            Jan 4 at 21:45



















          • Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !

            – kambo
            Jan 3 at 9: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 3 at 9:49






          • 1





            Please don't forget to accept the answer which solves your problem. It will help others :)

            – Nishu Tayal
            Jan 3 at 12:18











          • +1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers

            – Artem Bilan
            Jan 3 at 14:40











          • thank you, didn't know about that :)

            – kambo
            Jan 4 at 21:45

















          Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !

          – kambo
          Jan 3 at 9:43





          Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !

          – kambo
          Jan 3 at 9:43




          1




          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 3 at 9:49





          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 3 at 9:49




          1




          1





          Please don't forget to accept the answer which solves your problem. It will help others :)

          – Nishu Tayal
          Jan 3 at 12:18





          Please don't forget to accept the answer which solves your problem. It will help others :)

          – Nishu Tayal
          Jan 3 at 12:18













          +1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers

          – Artem Bilan
          Jan 3 at 14:40





          +1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers

          – Artem Bilan
          Jan 3 at 14:40













          thank you, didn't know about that :)

          – kambo
          Jan 4 at 21:45





          thank you, didn't know about that :)

          – kambo
          Jan 4 at 21:45




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019235%2fsending-using-kafkatemplatestring-string-returns-a-exception-for-using-string%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith