Home > OS >  KafkaController required a bean of type 'org.springframework.kafka.requestreply.ReplyingKafkaTe
KafkaController required a bean of type 'org.springframework.kafka.requestreply.ReplyingKafkaTe

Time:06-28

I am trying to use the RepliyngKafkaTemplate like I managed to use the KafkaTemplate in a REST controller.

@RestController
public class TestController {

    @Autowired
    private ReplyingKafkaTemplate<Object, KafkaExampleRecord, KafkaExampleRecord> replyingTemplate;

    @PostMapping("/test/request")
    public void requestReply(@RequestBody KafkaExampleRecord record) throws ExecutionException, InterruptedException, TimeoutException {
        ProducerRecord<Object, KafkaExampleRecord> producerRecord = new ProducerRecord<>("mytopic", record);
        RequestReplyFuture<Object, KafkaExampleRecord, KafkaExampleRecord> replyFuture = replyingTemplate.sendAndReceive(producerRecord);

        SendResult<Object, KafkaExampleRecord> sendResult = replyFuture.getSendFuture().get(10, TimeUnit.SECONDS);

        ConsumerRecord<Object, KafkaExampleRecord> consumerRecord = replyFuture.get(10, TimeUnit.SECONDS);
    }
}

However I am getting the following exception.

Field replyingTemplate in com.blah.KafkaController required a bean of type 'org.springframework.kafka.requestreply.ReplyingKafkaTemplate' that could not be found.

I enabled auto configuration like this.

@Configuration
@EnableKafka
public class KafkaConfig {

}

All Kafka settings are in my application.yml.

What else do I need? Do I really have to define beans? Seems unnecessary.

CodePudding user response:

Do I really have to define beans? Seems unnecessary.

Yes, you have to declare a beans for the replying template (including the reply container); Spring Boot only auto configures a simple KafkaTemplate.

CodePudding user response:

Can you check, whether you are scanning the basePackages correctly. Sometimes, you may end-up with this issue, if you not scanning the packages correctly, and I have experienced this many times in the Spring Boot application.

@ComponentScan(
        basePackages = {
                "x.x.x.x"
        }
)
  • Related