简体   繁体   English

Apache Camel Java DSL将类参数传递给bean方法

[英]Apache Camel Java DSL pass class parameter to bean method

I am using Camel 2.13.1 I want to pass a class as parameter to one of my methods in the bean 我正在使用骆驼2.13.1我想将类作为参数传递给Bean中的一种方法

Can I do something like 我可以做类似的事情吗

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

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

I know I can send the qualified class name in header and use it within the bean but it doesn't feel too right. 我知道我可以在标头中发送合格的类名,并在Bean中使用它,但是感觉并不正确。 Are there any other alternatives? 还有其他选择吗?

I saw this link but it did not work for me Apache Camel - Spring DSL - Pass String argument to bean method 我看到了此链接,但对我不起作用Apache Camel-Spring DSL-将String参数传递给bean方法

You can try using the wild card symbol '*'. 您可以尝试使用通配符'*'。 Camel will try to convert parameter to the correct type. 骆驼将尝试将参数转换为正确的类型。

Route: 路线:

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 context: 骆驼语境:

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");
}

Output: 输出:

class java.lang.String
class java.lang.String

A method parameter of type Class is not supported. 不支持Class类型的方法参数。 From the Camel documentation : 骆驼文档

Camel uses the following rules to determine if it's a parameter value in the method option 骆驼使用以下规则来确定它是否为方法选项中的参数值

  • The value is either true or false which denotes a boolean value 该值为true或false表示布尔值
  • The value is a numeric value such as 123 or 7 该值是一个数字值,例如123或7
  • The value is a String enclosed with either single or double quotes 该值是一个用单引号或双引号引起来的字符串
  • The value is null which denotes a null value 该值为null表示空值
  • It can be evaluated using the Simple language, which means you can use, eg, body, header.foo and other Simple tokens. 可以使用简单语言对其进行评估,这意味着您可以使用例如body,header.foo和其他简单标记。 Notice the tokens must be enclosed with ${ }. 请注意,令牌必须用$ {}括起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM