Spring Expression Language (SpEL)

http://camel.apache.org/spel.html

Spring Expression Language(SpEL)

Available as of Camel 2.7

Camel allows SpEL to be used as an Expression or Predicate in the DSL or Xml Configuration.

Variables

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

VariableTypeDescription
thisExchangethe Exchange is the root object
exchangeExchangethe Exchange object
exceptionThrowablethe Exchange exception (if any)
exchangeIdStringthe exchange id
faultMessagethe Fault message (if any)
requestMessagethe exchange.in message
responseMessagethe exchange.out message (if any)
propertiesMapthe exchange properties
property(name)Objectthe property by the given name
property(name, type)Typethe property by the given name as the given type

Samples

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");

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"

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'}
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");

Dependencies

You need Spring 3.0 or higher to use Spring Expression Language. If you use Maven you could just add the following to your pom.xml:

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-spring</artifactId>
  <version>xxx</version>
  <!-- use the same version as your Camel core version -->
</dependency>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值