【发布时间】:2017-04-03 22:46:27
【问题描述】:
我在从骆驼的 bean 中获取返回值并在我的路线中使用它时遇到了一些麻烦。
我的路线如下所示:
from(file:test/?delete=true)
.unmarshal(jaxb)
.bean(testBean, "testMethod")
.to(direct:nextRoute);
bean 看起来像这样:
public void testBean (PojoName pojoInstance){
//do stuff
int i= 75; //a number I generate within the bean after I've started this method
}
我想使用我在 bean 内部和路由中生成的数字。像这样的:
from(file:test/?delete=true)
.unmarshal(jaxb)
.bean(testBean, "testMethod")
.log(integer generated from testBean)
.to(direct:nextRoute);
我尝试了什么:
因此,我没有在我的 bean 中返回 void,而是将返回类型更改为 int 并返回整数。然后,我希望在我的路线中做这样的事情:
.log("${body.intFromBean}")
我的想法是,一旦我从 bean 返回值,它应该将该值存储在交换体中(至少这是我从 Camel 文档中得到的)。然后,我可以在我的路由中访问它。
问题:
但是,当我将 testBean 返回类型更改为 int 时,出现以下错误:
org.apache.camel.CamelExecutionException: Execution occurred during execution on the exchange
Caused by: org.apache.camel.InvalidPayloadException: No body available of type: PojoName but has value: numberIGenerated of type java.lang.Integer
(抱歉,我没有完整的堆栈跟踪。我正在使用 s.o 移动应用)
我的问题:
从阅读其他一些s.o.提交,我想我理解这个问题。消息体是一种类型,返回类型是另一种。但是,即使我尝试使用 .
.convertTo(Integer.class)
在调用 bean 之前,但这也不起作用。 (从概念上讲,这也行不通,因为如果我在解组后立即将其转换为 int,我将无法使用解组后的数据。但我想我还是会尝试一下)。
有人可以帮助我了解如何正确返回整数并在我的路由中使用它吗?
我已阅读有关 bean 绑定和交换的文档,并且我认为我对它的理解足以做到这一点。但我一定是错过了什么。
【问题讨论】:
-
完整的堆栈跟踪和 bean 的完整主体(包括 testMethod 实现)等会有所帮助,完整的路线也会有所帮助,testBean 定义在哪里等等。这对我来说也很不确定: ${body.intFromBean} 来自您的日志语句,但需要更多信息...
-
你试过用 .log("${body}") 代替 ${body.intFromBean} 吗?骆驼将结果存储为您的消息正文。
标签: java variables apache-camel