java中springboot集成kafka生產者Producer啓動報錯kafkaTemplate模板報錯

前言:之前博客裏面提到本公司爲物聯網項目。項目中使用mqtt+kafka進行與設備端的通訊,之前的協議格式爲json格式,現在改成字節數組byte[]格式進行通信。

  • 報錯信息如下
Can't convert value of class [B to class org.apache.kafka.common.serialization.StringSerializer specified in value.serializer

Field kafkaTemplateByte in com.hiss.producer.KafkaNewProducer required a bean of type 'org.springframework.kafka.core.KafkaTemplate' that could not be found.
- Bean method 'kafkaTemplate' in 'KafkaAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.kafka.core.KafkaTemplate; SearchStrategy: all) found bean 'kafkaTemplate'


Action:
Consider revisiting the conditions above or defining a bean of type 'org.springframework.kafka.core.KafkaTemplate' in your configuration.

  • 分析原因
    之前聲明KafkaTemplate時候value值爲String,後續改爲byte[]時候,未將KafkaTemplate的value類型改爲byte[]類型。

  • 解決辦法

 public KafkaTemplate<String, byte[]> kafkaTemplateByte() {
    	return new KafkaTemplate<String,byte[]>(producerFactoryByte());
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章