I am trying to set a header parameter (param2), using simple expression language:

from("timer://foo?repeatCount=1")
    .setBody(constant("on,off"))
    .split()
    .tokenize(",")
    .setHeader("param1", simple("${body}"))
    .setHeader("param2", simple("${header.param1} == 'on'"))
    .log("param1 = [${header.param1}], param2 = [${header.param2}]");

But as result I get a string set instead of a boolean value, the following gets logged:

param1 = [on], param2 = [on == 'on']
param1 = [off], param2 = [off == 'on']

I would expect it log:

param1 = [on], param2 = [true]
param1 = [off], param2 = [false]

Documentation writes, that it is possible to do comparison using simple and I can get it working when doing choices:

from("timer://foo?repeatCount=1")
    .setBody(constant("on,off"))
    .split()
    .tokenize(",")
    .setHeader("param1", simple("${body}"))
    .choice()
        .when(simple("${header.param1} == 'on'"))
            .setHeader("param2", constant(true))
        .otherwise()
            .setHeader("param2", constant(false))
        .end()
    .log("param1 = [${header.param1}], param2 = [${header.param2}]");

The output then is:

param1 = [on], param2 = [true]
param1 = [off], param2 = [false]

But why it is not working then in direct assignment?

有帮助吗?

解决方案

This is a bug. I tested with 2.12.3, 2.13.0, 2.12-SNAPSHOT and 2.13-SNAPSHOT and the operators are not handled correctly. Even setting the output type did not help.

By the way, you may also use Groovy to solve your problem:

 .setHeader("param2").groovy("request.headers.param1.equals('on')")

EDIT: This bug was fixed in 2.12.4, 2.13.1 and 2.14.0, see CAMEL-7298

其他提示

You can define the result type of à simple expression, like so

.setHeader("param2", simple("${header.param1} == 'on'", Boolean.class))

Another way would be using an expression language like MVEL. Use the camel mvel component and write your route as

        from("timer://foo?repeatCount=1")
            .setBody(constant("on,off"))
            .split()
            .tokenize(",")
            .setHeader("param1", simple("${body}"))
            .setHeader("param2").mvel("request.headers.param1 == 'on'")
            .log("param1 = [${header.param1}], param2 = [${header.param2}]");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top