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:
<language language="simple">${body.address}</language> <language language="simple">${body.address.street}</language> <language language="simple">${body.address.zip}</language> <language language="simple">${body.address.city}</language>
Where the notation, ${body.address.street}
, is shorthand for
${body.getAddress.getStreet}
.
You can use the null-safe operator, ?.
, to avoid encountering
null-pointer exceptions, in case the body does not have an
address. For example:
<language language="simple">${body?.address?.street}</language>
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:
<language language="simple">simple("${body[foo]?.name}")</language>
You can also use square brackets notation, [k]
, to access the
elements of a list. For example:
<language language="simple">${body.address.lines[0]}</language> <language language="simple">${body.address.lines[1]}</language> <language language="simple">${body.address.lines[2]}</language>
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:
<language language="simple">${body.address.lines[last-1]}</language>