【问题标题】:Spring boot kafka hello world app fails to start with "Failed to bind properties under...to..."Spring boot kafka hello world 应用程序无法以“无法将属性绑定在...下...”
【发布时间】:2020-02-14 06:44:11
【问题描述】:

我正在使用 this 教程尝试基本的 Spring Boot kafka 应用程序。

我的项目层次结构如下所示:

SpringBootKafkaApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootKafkaApplication{
    public static void main(String[] args) {
        SpringApplication.run(SpringBootKafkaApplication.class, args);
    }
}

KafkaController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.temp.kafka_springboot_pocs.services.Producer;

@RestController
@RequestMapping(value = "/kafka")
public class KafkaController {

    private final Producer producer;

    @Autowired
    public KafkaController(Producer producer) {
        this.producer = producer;
    }

    @PostMapping(value = "/publish")
    public void sendMessageToKafkaTopic(@RequestParam("message") String message) {
        this.producer.sendMessage(message);
    }
}

Consumer.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class Consumer {

    private final Logger logger = LoggerFactory.getLogger(Consumer.class);

    @KafkaListener(topics = "kafka_poc_3", groupId = "group_id")
    public void consume(String message) {
        logger.info(String.format("$$ -> Consumed Message -> %s", message));
    }
}

Producer.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class Producer {

    private static final Logger logger = LoggerFactory.getLogger(Producer.class);
    private static final String TOPIC = "kafka_poc_3";

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendMessage(String message) {
        logger.info(String.format("$$ -> Producing message --> %s",message));
        this.kafkaTemplate.send(TOPIC, message);
    }
}

application.properties

spring.kafka.bootstrap-servers=localhost:9092

spring.kafka.consumer.group-id=group-1
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserialize

spring.kafka.producer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.producer.value-deserializer=org.apache.kafka.common.serialization.StringDeserialize

Spring Boot 应用程序启动失败,错误描述如下:

说明:

Failed to bind properties under 'spring.kafka.consumer.value-deserializer' to java.lang.Class<?>:

    Property: spring.kafka.consumer.value-deserializer
    Value: org.apache.kafka.common.serialization.StringDeserialize
    Origin: class path resource [application.properties]:12:42
    Reason: No converter found capable of converting from type [java.lang.String] to type [java.lang.Class<?>]

我在这里缺少什么?

PS:我已经很久没穿弹簧和弹簧靴了。所以会喜欢有人给我很好的方向/链接的例子或确切的答案。

【问题讨论】:

  • 我认为您需要在配置中添加消息转换器或将字符串消息更改为字节数组
  • 也许您只是在StringDeserialize 之后缺少了一个“r”。
  • 哎呀..这确实是个错误!!! ??????????

标签: java spring spring-boot apache-kafka


【解决方案1】:

试试这个属性

#KAFKA 制作人设置

spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer

#KAFKA 消费者设置

spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer spring.kafka.consumer.auto-offset-reset=最早

【讨论】:

    猜你喜欢
    • 2018-10-17
    • 2020-11-27
    • 2016-04-25
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 2021-11-11
    相关资源
    最近更新 更多