...
SpEL expressions need to be surrounded by #{
}
delimiters since expression templating is enabled. This allows you to combine SpEL expressions with regular text and use this as extremely lightweight template language.
...
Code Block |
---|
from("direct:example").setBody(spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}")).to("mock:result"); |
In the route above, notice spel is a static method which we need to import from org.apache.camel.language.spel.SpelExpression.spel
, as we use spel as an Expression passed in as a parameter to the setBody
method. Though if we use the fluent API we can do this instead:
Code Block |
---|
from("direct:example").setBody().spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}").to("mock:result");
|
Notice we now use the spel
method from the setBody()
method. And this does not require us to static import the spel method from org.apache.camel.language.spel.SpelExpression.spel
.
And sent a message with the string "World" in the body, and a header "dayOrNight" with value "day":
...