【问题标题】:Apache Camel Java DSL pass class parameter to bean methodApache Camel Java DSL将类参数传递给bean方法
【发布时间】:2015-01-02 22:04:48
【问题描述】:
我正在使用 Camel 2.13.1 我想将一个类作为参数传递给我在 bean 中的一个方法
我可以做类似的事情
In Route
--
.beanRef("someSpringBeanRef","someMethod(${body},com.test.TestObject)")
--
And in Bean
public Object someMethod(String testBody, Class type){
我知道我可以在 header 中发送合格的类名并在 bean 中使用它,但感觉不太对劲。还有其他选择吗?
我看到了这个链接,但它对我不起作用
Apache Camel - Spring DSL - Pass String argument to bean method
【问题讨论】:
标签:
java
spring
apache-camel
enterprise-integration
【解决方案1】:
您可以尝试使用通配符“*”。 Camel 会尝试将参数转换为正确的类型。
路线:
public class Routes extends RouteBuilder {
public void configure() throws Exception {
from("direct:in").bean(new TestBean(), "test(*, ${body})");
}
}
豆子:
public class TestBean {
public void test(Class<?> clazz, String str) {
System.out.println(clazz);
}
}
骆驼上下文:
public static void main(String[] args) throws Exception {
CamelContext ctx = new DefaultCamelContext();
ctx.addRoutes(new Routes());
ctx.start();
ctx.createProducerTemplate().sendBody("direct:in", String.class);
ctx.createProducerTemplate().sendBody("direct:in", "java.lang.String");
}
输出:
class java.lang.String
class java.lang.String
【解决方案2】:
不支持Class 类型的方法参数。来自Camel documentation:
Camel使用以下规则来判断是否是方法选项中的参数值
- 该值为 true 或 false,表示布尔值
- 该值是一个数值,例如 123 或 7
- 值是用单引号或双引号括起来的字符串
- 值为null,表示为空值
- 可以使用 Simple 语言对其进行评估,这意味着您可以使用例如 body、header.foo 和其他 Simple 标记。请注意,标记必须用 ${ } 括起来。