我正在使用 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
最佳答案
您可以尝试使用通配符“*”。 Camel 会尝试将参数转换为正确的类型。
路线:
public class Routes extends RouteBuilder {
public void configure() throws Exception {
from("direct:in").bean(new TestBean(), "test(*, ${body})");
}
}
bean :
public class TestBean {
public void test(Class<?> clazz, String str) {
System.out.println(clazz);
}
}
Camel 上下文:
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
关于java - Apache Camel Java DSL 将类参数传递给 bean 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26778800/