Este conteúdo não está disponível no idioma selecionado.

Chapter 31. SpEL


Overview

The Spring Expression Language (SpEL) is an object graph navigation language provided with Spring 3, which can be used to construct predicates and expressions in a route. A notable feature of SpEL is the ease with which you can access beans from the registry.

Syntax

The SpEL expressions must use the placeholder syntax, #{SpelExpression}, so that they can be embedded in a plain text string (in other words, SpEL has expression templating enabled).
SpEL can also look up beans in the registry (typically, the Spring registry), using the @BeanID syntax. For example, given a bean with the ID, headerUtils, and the method, count() (which counts the number of headers on the current message), you could use the headerUtils bean in an SpEL predicate, as follows:
#{@headerUtils.count > 4}
Copy to Clipboard Toggle word wrap

Adding SpEL package

To use SpEL in your routes you need to add a dependency on camel-spring to your project as shown in Example 31.1, “Adding the camel-spring dependency”.

Example 31.1. Adding the camel-spring dependency

<!-- Maven POM File -->
<properties>
  <camel-version>2.15.1.redhat-621084</camel-version>
  ...
</properties>

<dependencies>
  ...
  <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring</artifactId>
    <version>${camel-version}</version>
  </dependency>
  ...
</dependencies>
Copy to Clipboard Toggle word wrap

Variables

Table 31.1, “SpEL variables” lists the variables that are accessible when using SpEL.
Expand
Table 31.1. SpEL variables
VariableTypeDescription
thisExchangeThe current exchange is the root object.
exchangeExchangeThe current exchange.
exchangeIdStringThe current exchange's ID.
exceptionThrowableThe exchange exception (if any).
faultMessageThe fault message (if any).
requestMessageThe exchange's In message.
responseMessageThe exchange's Out message (if any).
propertiesMapThe exchange properties.
property(Name)ObjectThe exchange property keyed by Name.
property(Name, Type)TypeThe exchange property keyed by Name, converted to the type, Type.

XML example

For example, to select only those messages whose Country header has the value USA, you can use the following SpEL expression:
<route>
  <from uri="SourceURL"/>
  <filter>
    <spel>#{request.headers['Country'] == 'USA'}}</spel>
    <to uri="TargetURL"/>
  </filter>
</route>
Copy to Clipboard Toggle word wrap

Java example

You can define the same route in the Java DSL, as follows:
from("SourceURL")
  .filter().spel("#{request.headers['Country'] == 'USA'}")
  .to("TargetURL");
Copy to Clipboard Toggle word wrap
The following example shows how to embed SpEL expressions within a plain text string:
from("SourceURL")
  .setBody(spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}"))
  .to("TargetURL");
Copy to Clipboard Toggle word wrap
Voltar ao topo
Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2025 Red Hat