spring - 如何将请求数据从 Spring Boot Controller 传递到 apache Camel 路由

标签 spring spring-boot apache-camel

用例:在我的应用程序中有一个REST Controller ,在Spring boot的帮助下开发,我的要求是,我必须将请求数据从 Controller 传递到路由,再次需要从路由将数据传递给MQ

这里如何将 inputReq 数据从 Controller 传递到路由?有人可以帮忙吗

@Controller
public class RequestController {

    @PostMapping("/request")
    public String requestMapping(@RequestBody String inputReq) {
        new ProduceRouter();  // instance of the apache camel route
        return null;

    }
}

下面是 apache Camel 路线:

@Component
public class ProduceRouter extends RouteBuilder {

    @Override
    public void configure() throws Exception {
            
        .from("jms:RequestQueue?disableReplyTo=true")
        .log("Received Body is  ${body}   and header info is   ${headers}  ");

        

    }
}

最佳答案

在您的 Controller 中, Autowiring CamelContext 和 ProducerTemplate 的实例。

@Autowired
private CamelContext camelContext;
@Autowired
private ProducerTemplate producer;

然后,您需要使用 ExchangeBuilder 创建交换请求并添加您的请求正文。

Exchange exchangeRequest = ExchangeBuilder.anExchange(camelContext)
.withBody(inputReq).build();

然后,您可以调用生产者对象上的 send 方法来进入您的路由并捕获响应。

Exchange exchangeResponse = producer.send("direct:startRoute", exchangeRequest).

然后在你的路由文件中,你可以使用 direct:startRoute

关于spring - 如何将请求数据从 Spring Boot Controller 传递到 apache Camel 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63373252/

相关文章:

java - Spring Cloud Kubernetes 应用程序未检测到它何时在 pod 中运行

xml - 与元素类型 "xsi"关联的属性 "xsi:schemaLocation"的前缀 "beans"未绑定(bind)。在tomcat上运行spring时

spring-boot - Spring 数据 JPA。从 Pageable 获取所有页面

java - 无法将字符串导出为 .xlsx 文件中的超链接 - Java 8 和 POI 3.17

spring - 为什么 application.properties 中的属性在 Spring Boot 应用程序的 Junit 测试中不可用?

java - 每个端点 URI 的 ProducerTemplate?

ios - 我的 Xcode 应用程序图标项目中出现齿轮图标,我无法再运行它

java - 手动将 Spring MVC 项目导入 Eclipse

java - 如何让apache Camel成为WS客户端和服务器之间的桥梁?

apache-camel - Apache Camel 如何从 sql 组件访问 Header 值