이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 295. SpEL Language


Available as of Camel version 2.7

Camel allows Spring Expression Language (SpEL) to be used as an Expression or Predicate in the DSL or XML Configuration.

Note

It is recommended to use SpEL in Spring runtimes. However from Camel 2.21 onwards you can use SpEL in other runtimes (there may be functionality SpEL cannot do when not running in a Spring runtime)

295.1. Variables

The following variables are available in expressions and predicates written in SpEL:

VariableTypeDescription

this

Exchange

the Exchange is the root object

exchange

Exchange

the Exchange object

exception

Throwable

the Exchange exception (if any)

exchangeId

String

the exchange id

fault

Message

the Fault message (if any)

body

Object

The IN message body.

request

Message

the exchange.in message

response

Message

the exchange.out message (if any)

properties

Map

the exchange properties

property(name)

Object

the property by the given name

property(name, type)

Type

the property by the given name as the given type

295.2. Options

The SpEL language supports 1 options which are listed below.

NameDefaultJava TypeDescription

trim

true

Boolean

Whether to trim the value to remove leading and trailing whitespaces and line breaks

295.3. Samples

295.3.1. Expression templating

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.

For example if you construct the following route:

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:

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":

template.sendBodyAndHeader("direct:example", "World", "dayOrNight", "day");

The output on mock:result will be "Hello World! What a beautiful day"

295.3.2. Bean integration

You can reference beans defined in the Registry (most likely an ApplicationContext) in your SpEL expressions. For example if you have a bean named "foo" in your ApplicationContext you can invoke the "bar" method on this bean like this:

#{@foo.bar == 'xyz'}

295.3.3. SpEL in enterprise integration patterns

You can use SpEL as an expression for Recipient List or as a predicate inside a Message Filter:

<route>
  <from uri="direct:foo"/>
  <filter>
    <spel>#{request.headers['foo'] == 'bar'}</spel>
    <to uri="direct:bar"/>
  </filter>
</route>

And the equivalent in Java DSL:

from("direct:foo")
    .filter().spel("#{request.headers['foo'] == 'bar'}")
    .to("direct:bar");

295.4. Loading script from external resource

Available as of Camel 2.11

You can externalize the script and have Camel load it from a resource such as "classpath:", "file:", or "http:".
This is done using the following syntax: "resource:scheme:location", eg to refer to a file on the classpath you can do:

.setHeader("myHeader").spel("resource:classpath:myspel.txt")
Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.