【问题标题】:Apache Camel Spring Boot Java - how can I pass a dynamic value from a rest route to a from file route?Apache Camel Spring Boot Java - 如何将动态值从休息路由传递到来自文件的路由?
【发布时间】:2021-06-24 21:40:58
【问题描述】:

我有一个 Apache Camel REST 路由。我想从Postman 调用这个 REST 发布路由,并且我想向它发送包含文件路径的 JSON 内容。然后我想读取该文件路径以在路由 from file route 中使用它。

到目前为止,我有这个:

@Component
    class RestApi extends RouteBuilder {
        @Override
        public void configure() {
            CamelContext context = new DefaultCamelContext();

            restConfiguration()
                    .contextPath(contextPath)
                    .port(serverPort)
                    .enableCORS(true)
                    .apiContextPath("/api-doc")
                    .apiProperty("api.title", "Test REST API")
                    .apiProperty("api.version", "v1")
                    .apiContextRouteId("doc-api")
                    .component("servlet")
                    .bindingMode(RestBindingMode.json);
            rest("/api/")
                    .id("api-route")
                    .consumes("application/json")
                    .post("/bean")
                    .bindingMode(RestBindingMode.json_xml)
                    .type(MyBean.class)
                    .to("direct:remoteService");
            from("direct:remoteService")
                    .routeId("direct-route")
                    .tracing()
                    .log(">>> ${body.id}")
                    .log(">>> ${body.name}")

                    // I tried calling it with "toD"

但它不起作用。

                    .toD("file://${body.name}?fileName=sources.zip&noop=true&delay=5000&moveFailed=error");
            from("file://${body.name}?fileName=sources.zip&noop=true&delay=5000&moveFailed=error")
                    .tracing()
                    .log(">>> ${body.id}")
                    .log(">>> ${body.name}")
                    .log("Loading zip file ${file:name}")
                    .split(new ZipSplitter())
                    .streaming()
                    .to("direct:another-route");
        }
    }

我尝试使用 toD,但出现以下错误:

原因:java.lang.IllegalArgumentException:无效目录:${body.name}。不允许使用带有 ${ } 占位符的动态表达式。使用 fileName 选项设置动态表达式。

我想获得用 Postman body.name 发送的路径。我可以得到该值,因为它显示在日志中,然后使用该路径启动 from 文件路径路由,

from("file://${body.name}?fileName=sources.zip&noop=true&delay=5000&moveFailed=error")
                    .tracing()
                    .log(">>> ${body.id}")
                    .log(">>> ${body.name}")
                    .log("Loading zip file ${file:name}")
                    .split(new ZipSplitter())
                    .streaming()
                    .to("direct:another-route");

编辑

我尝试使用 pollEncrich,但我需要传递一个变量,即我从帖子路由 ${body.name} 获得的变量。如何将该变量传递给 pollEnrich 的路由?

我试过这个,但它不起作用:

rest("/api/")
                    .id("api-route")
                    .consumes("application/json")
                    .post("/bean")
                    .bindingMode(RestBindingMode.json_xml)
                    .type(MyBean.class)
                    .to("direct:remoteService");
            from("direct:remoteService")
                    .routeId("direct-route")
                    .tracing()
                    .log(">>> ${body.id}")
                    .log(">>> ${body.name}")
                    .pollEnrich("file://${body.name}?fileName=sources.zip&noop=true&delay=5000&moveFailed=error")
                    .split(new ZipSplitter())
                    .streaming()
                    .to("direct:process-files");

我收到以下错误:

无效目录:${body.name}。不允许使用带有 ${ } 占位符的动态表达式。使用 fileName 选项设置动态表达式。

【问题讨论】:

    标签: java spring spring-boot apache-camel java-11


    【解决方案1】:

    查看内容丰富器 EIP(文件上的 to 和 toD 是作为新文件写入的)。您需要使用 pollEnrich(内容丰富器)来加载现有文件。

    【讨论】:

    • 谢谢,我使用 pollEnrich 但我收到以下错误 Invalid directory: ${body.name}. Dynamic expressions with ${ } placeholders is not allowed. Use the fileName option to set the dynamic expression. 我需要将变量传递给该路由,我尝试使用 ${body.name} 但不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 2020-07-01
    • 2018-07-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多