1

I am using Camel 2.13.1 I want to pass a class as parameter to one of my methods in the 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. 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

Community
  • 1
  • 1
Richie
  • 9,006
  • 5
  • 25
  • 38

2 Answers2

2

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
kaos
  • 1,598
  • 11
  • 15
1

A method parameter of type Class is not supported. 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
  • The value is a numeric value such as 123 or 7
  • The value is a String enclosed with either single or double quotes
  • The value is null which denotes a null value
  • It can be evaluated using the Simple language, which means you can use, e.g., body, header.foo and other Simple tokens. Notice the tokens must be enclosed with ${ }.
Peter Keller
  • 7,526
  • 2
  • 26
  • 29