Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.이 콘텐츠는 선택한 언어로 제공되지 않습니다.
29.4. Expressions
Overview 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
The simple language provides various elementary expressions that return different parts of a message exchange. For example, the expression,
simple("${header.timeOfDay}")
, would return the contents of a header called timeOfDay
from the incoming message.
Note
Since Apache Camel 2.9, you must always use the placeholder syntax,
${Expression}
, to return a variable value. It is never permissible to omit the enclosing tokens (${
and }
).
Contents of a single variable 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
You can use the simple language to define string expressions, based on the variables provided. For example, you can use a variable of the form,
in.header.
HeaderName, to obtain the value of the HeaderName header, as follows:
simple("${in.header.foo}")
simple("${in.header.foo}")
Variables embedded in a string 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
You can embed simple variables in a string expression—for example:
simple("Received a message from ${in.header.user} on ${date:in.header.date:yyyyMMdd}.")
simple("Received a message from ${in.header.user} on ${date:in.header.date:yyyyMMdd}.")
date and bean variables 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
As well as providing variables that access all of the different parts of an exchange (see Table 29.1, “Variables for the Simple Language”), the simple language also provides special variables for formatting dates,
date:
command:
pattern, and for calling bean methods, bean:
beanRef. For example, you can use the date and the bean variables as follows:
simple("Todays date is ${date:now:yyyyMMdd}") simple("The order type is ${bean:orderService?method=getOrderType}")
simple("Todays date is ${date:now:yyyyMMdd}")
simple("The order type is ${bean:orderService?method=getOrderType}")
Specifying the result type 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
You can specify the result type of an expression explicitly. This is mainly useful for converting the result type to a boolean or numerical type.
In the Java DSL, specify the result type as an extra argument to
simple()
. For example, to return an integer result, you could evaluate a simple expression as follows:
... .setHeader("five", simple("5", Integer.class))
...
.setHeader("five", simple("5", Integer.class))
In the XML DSL, specify the result type using the
resultType
attribute. For example:
<setHeader headerName="five"> <!-- use resultType to indicate that the type should be a java.lang.Integer --> <simple resultType="java.lang.Integer">5</simple> </setHeader>
<setHeader headerName="five">
<!-- use resultType to indicate that the type should be a java.lang.Integer -->
<simple resultType="java.lang.Integer">5</simple>
</setHeader>
Nested expressions 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
Simple expressions can be nested—for example:
simple("${header.${bean:headerChooser?method=whichHeader}}")
simple("${header.${bean:headerChooser?method=whichHeader}}")
Accessing constants or enums 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
You can access a bean's constant or enum fields using the following syntax:
type:ClassName.Field
type:ClassName.Field
For example, consider the following Java
enum
type:
package org.apache.camel.processor; ... public enum Customer { GOLD, SILVER, BRONZE }
package org.apache.camel.processor;
...
public enum Customer {
GOLD, SILVER, BRONZE
}
You can access the
Customer
enum fields, as follows:
OGNL expressions 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
The Object Graph Navigation Language (OGNL) is a notation for invoking bean methods in a chain-like fashion. If a message body contains a Java bean, you can easily access its bean properties using OGNL notation. For example, if the message body is a Java object with a
getAddress()
accessor, you can access the Address
object and the Address
object's properties as follows:
simple("${body.address}") simple("${body.address.street}") simple("${body.address.zip}") simple("${body.address.city}")
simple("${body.address}")
simple("${body.address.street}")
simple("${body.address.zip}")
simple("${body.address.city}")
Where the notation,
${body.address.street}
, is shorthand for ${body.getAddress.getStreet}
.
OGNL null-safe operator 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
You can use the null-safe operator,
?.
, to avoid encountering null-pointer exceptions, in case the body does not have an address. For example:
simple("${body?.address?.street}")
simple("${body?.address?.street}")
If the body is a
java.util.Map
type, you can look up a value in the map with the key, foo
, using the following notation:
simple("${body[foo]?.name}")
simple("${body[foo]?.name}")
OGNL list element access 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
You can also use square brackets notation,
[k]
, to access the elements of a list. For example:
simple("${body.address.lines[0]}") simple("${body.address.lines[1]}") simple("${body.address.lines[2]}")
simple("${body.address.lines[0]}")
simple("${body.address.lines[1]}")
simple("${body.address.lines[2]}")
The
last
keyword returns the index of the last element of a list. For example, you can access the second last element of a list, as follows:
simple("${body.address.lines[last-1]}")
simple("${body.address.lines[last-1]}")
You can use the
size
method to query the size of a list, as follows:
simple("${body.address.lines.size}")
simple("${body.address.lines.size}")
OGNL array length access 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
You can access the length of a Java array through the
length
method, as follows:
String[] lines = new String[]{"foo", "bar", "cat"}; exchange.getIn().setBody(lines); simple("There are ${body.length} lines")
String[] lines = new String[]{"foo", "bar", "cat"};
exchange.getIn().setBody(lines);
simple("There are ${body.length} lines")