294.3. サンプル
294.3.1. 式のテンプレート リンクのコピーリンクがクリップボードにコピーされました!
式のテンプレートが有効にされているため、SpEL 式を #{ } 区切り文字で囲む必要があります。これにより、SpEL 式と正規表現を組み合わせて、非常に軽量のテンプレート言語として使用できます。
たとえば、以下のルートを作成する場合は、以下のようになります。
from("direct:example")
.setBody(spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}"))
.to("mock:result");
from("direct:example")
.setBody(spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}"))
.to("mock:result");
上記のルートでは、spel は、org.apache.camel.language.spelExpression.spelExpression.spel からインポートする必要のある static メソッドです。これは、spel を、setBody メソッドにパラメーターとして渡される式として使用するためです。Fluent API を使用する場合は、代わりにこれを行うことができます。
from("direct:example")
.setBody().spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}")
.to("mock:result");
from("direct:example")
.setBody().spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}")
.to("mock:result");
setBody() メソッドの spel メソッドを使用することに注意してください。また、org.apache.camel.language.spelExpression.spelExpression.spelExpression.spel から spel メソッドを静的にインポートする必要はありません。
そして、本文に文字列 "gitops" と、値が "day" のヘッダー "dayOrNight" を持つメッセージを送信します。
template.sendBodyAndHeader("direct:example", "World", "dayOrNight", "day");
template.sendBodyAndHeader("direct:example", "World", "dayOrNight", "day");
mock:result の出力は "Hello World! になります。異常な日"