骆驼路线中缺少参数的默认值
Default value for missing parameter in a Camel route
我有一个使用 Apache Camel 和 Active MQ 的应用程序。在我配置路由的 RouteBuilder
class 中,in
正文中可能缺少一个参数。在那种情况下,我想给它一个默认值。
这是我现在拥有的:
@Override
public void configure() throws Exception {
...
String invocation = "aMethod( ${body[context]}, ${body[aParam]}, ${body[param2]} )";
from(url).routeId(url).bean(bean, invocation);
}
在我的例子中,param2
是一个布尔值,我想将其作为 false
转发给 aMethod
,以防它不存在。我该怎么做?
我在这里看到了一些东西:http://camel.apache.org/simple.html#Simple-OGNLexpressionsupport 上面写着 "You can also use the null safe operator (?.) to avoid NPE if for example the body does NOT have an address",但我不知道如何编写调用,所以我没有在指定的情况下有错误。
我相信您希望使用与此类似的条件:
from("")
.choice()
.when(header("myHeader").isNull())
.setHeader("myHeader").simple("false")
.end() //This might be endChoice can't remember syntax exactly
.bean(myBean, myMethod);
..为正文中的对象修改
from("")
.processor( new Processor(Exchange exchange) {
MyObject obj = exchange.getIn().getBody(MyObject.class);
Boolean b = obj.getParam2();
if(b == null)
obj.setParam2(Boolean.FALSE);
}
.bean(myBean, myMethod);
注意:如果您的实际 java class 使用 'boolean' 而不是 'Boolean' class 包装器,则默认情况下会设置初始化时的所有布尔值假的。因此,如果您体内有一个对象,但没有人拥有布尔值,它将默认为 false。
我有一个使用 Apache Camel 和 Active MQ 的应用程序。在我配置路由的 RouteBuilder
class 中,in
正文中可能缺少一个参数。在那种情况下,我想给它一个默认值。
这是我现在拥有的:
@Override
public void configure() throws Exception {
...
String invocation = "aMethod( ${body[context]}, ${body[aParam]}, ${body[param2]} )";
from(url).routeId(url).bean(bean, invocation);
}
在我的例子中,param2
是一个布尔值,我想将其作为 false
转发给 aMethod
,以防它不存在。我该怎么做?
我在这里看到了一些东西:http://camel.apache.org/simple.html#Simple-OGNLexpressionsupport 上面写着 "You can also use the null safe operator (?.) to avoid NPE if for example the body does NOT have an address",但我不知道如何编写调用,所以我没有在指定的情况下有错误。
我相信您希望使用与此类似的条件:
from("")
.choice()
.when(header("myHeader").isNull())
.setHeader("myHeader").simple("false")
.end() //This might be endChoice can't remember syntax exactly
.bean(myBean, myMethod);
..为正文中的对象修改
from("")
.processor( new Processor(Exchange exchange) {
MyObject obj = exchange.getIn().getBody(MyObject.class);
Boolean b = obj.getParam2();
if(b == null)
obj.setParam2(Boolean.FALSE);
}
.bean(myBean, myMethod);
注意:如果您的实际 java class 使用 'boolean' 而不是 'Boolean' class 包装器,则默认情况下会设置初始化时的所有布尔值假的。因此,如果您体内有一个对象,但没有人拥有布尔值,它将默认为 false。