1

我正在使用 Camel 2.13.1 我想将一个类作为参数传递给我在 bean 中的一个方法

我可以做类似的事情吗

In Route
    --
     .beanRef("someSpringBeanRef","someMethod(${body},com.test.TestObject)")
    --

And in Bean
      public Object someMethod(String testBody, Class type){

我知道我可以在标头中发送合格的类名并在 bean 中使用它,但感觉不太对劲。还有其他选择吗?

我看到了这个链接,但它对我不起作用 Apache Camel - Spring DSL - 将字符串参数传递给 bean 方法

4

2 回答 2

2

您可以尝试使用通配符“*”。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
于 2014-11-07T12:39:18.080 回答
1

Class不支持类型的方法参数。从骆驼文档

Camel 使用以下规则来确定它是否是方法选项中的参数值

  • 该值为真或假,表示布尔值
  • 该值是一个数值,例如 123 或 7
  • 该值是用单引号或双引号括起来的字符串
  • 该值为空,表示空值
  • 它可以使用 Simple 语言进行评估,这意味着您可以使用例如 body、header.foo 和其他 Simple 标记。请注意,标记必须用 ${ } 括起来。
于 2014-11-06T21:54:13.797 回答