骆驼脚本没有 运行

Camel script does not run

我有一个非常简单的骆驼流程来测试编写脚本。它看起来像这样:

from("file://C:test?fileName=in.xml").routeId("ContentbasedRouter")
.choice()       
   .when(toXML)
        .script().javaScript("request.body.substring(3, 6)")

但是当我 运行 我得到

org.apache.camel.builder.script.ScriptEvaluationException: Failed to evaluate: js: request.body.substring(3, 6). Cause: <eval>:1 TypeError: GenericFile[C:\test\input.csv] has no such function "substring"

我的意思是 substring 是一个有效的 javascript 函数,为什么它不能识别这个?

file 消费者发送与 GenericFile 类型正文的交换,而不是字符串。如果你想要文件名,你可以使用方法 GenericFile.getFileName() :

from("file://C:test?fileName=in.xml").routeId("ContentbasedRouter")
.choice()       
   .when(toXML)
        .script().javaScript("request.body.fileName.substring(3, 6)")

如果你想要文件的内容,你可以把它转换成字符串:

from("file://C:test?fileName=in.xml").routeId("ContentbasedRouter")
.convertBodyTo(String.class)
.choice()       
   .when(toXML)
        .script().javaScript("request.body.substring(3, 6)")

GenericFile