消费者根据生产者发送的消息触发api

时间:2021-06-14 17:37:15

标签: java spring-boot apache-kafka

我使用 spring 制作了一个消费者和生产者类。现在我想让消费者根据生产者发送的消息触发一些 api。怎么做? 请在 JAVA SpringBoot 中提供解决方案。 如何从消费者的 application.yml 触发 api?

@Service
public class KafkaConsumer{
   
    @KafkaListener(topics="mytopic", groupID="mygroup")
    public void receive(String message, Acknowledgement acknowledgement)){
           System.out.println("Consumer message "+ message);
    }
    // when I add @postMapping here then it gives error. 
    // How can I send a post request on the basis of message recieved?
}

1 个答案:

答案 0 :(得分:0)

<块引用>

当我在这里添加 @postMapping 时,它会出错

您只能在处理传入请求的 REST 服务器方法上添加该注释。

您正在尝试进行 传出 HTTP 调用,那么您需要使用您选择的 HTTP 客户端,或 Spring RestTemplate

如果您尝试调用任何内部 HTTP 端点,那么您应该重构代码以调用与这些 HTTP 资源交互的相同类的方法。