Set Property
The Set Property EIP is used in the context of message exchanges. Properties are useful for storing intermediate data that can be accessed in other steps of the flow.
Properties are metadata associated with the message but are neither part of the body nor the header. |
Parameters
Parameter | Description | Default value | Type |
---|---|---|---|
Name |
Name of the property to be configured. |
String |
|
Expression |
Expression that returns the property’s value. |
ExpressionDefinition |
Expression
The expression allows dynamically calculating or defining values during message processing in the integration flow. It is a way to configure behaviors that may vary based on the context of the in-transit message.
The expression is organized into two hierarchical levels, enabling a more extensible and adaptable structure for different contexts:
-
First level: specifies the type of logic to be applied (constant, simple, jsonpath, etc.).
-
Second level: defines the actual value of the expression, that is, the data to be assigned to the property.
Common types of expressions
-
constant
: assigns fixed values. In this example, themyProperty
property is set toFixedValue
.
- setProperty:
name: myProperty
expression:
constant: "FixedValue"
-
simple
: accesses simple data (headers, body, properties) and performs basic value manipulations. For instance, the expression${header.orderId}
accesses theorderId
header and uses it as the value for themyProperty
property.
- setProperty:
name: myProperty
expression:
simple: ${header.orderId}
-
xpath
: uses XPath queries to extract data from XML documents. For example, the XPath expression/order/id
extracts the value of theid
field from an XML structure like<order><id>123</id></order>
.
- setProperty:
name: myProperty
expression:
xpath: "/order/id"
-
jsonpath
: uses JSONPath queries to extract data from JSON documents. For instance, the expression$.order.id
extracts the value of theid
field within a JSON object:{ "order": { "id": 123 } }
.
- setProperty:
name: myProperty
expression:
jsonpath: "$.order.id"
-
groovy
: executes Groovy scripts for advanced data manipulation. In the example, the value ofmyProperty
is calculated by a Groovy script that increments the value of theorderId
header by 1.
- setProperty:
name: myProperty
expression:
groovy: "return message.getHeader('orderId') + 1"
Examples
Example 1
- setProperty:
name: itemId
expression:
simple: ${body['id']}
-
name: itemId
: name of the property to be set. The property will be referenced asitemId
in subsequent steps of the route. -
expression: simple: ${body['id']}
: uses the simple language to extract data from the message body. In this case, it accesses a field namedid
within a structure like a map (JSON or similar). The value extracted from theid
field is assigned to theitemId
property.For example, if the message body is:
{ "id": "12345", "name": "Sample Item" }
-
The Camel route evaluates the expression
${body['id']}
and extracts the value12345
from theid
field, storing it in the property nameditemId
.
-
Example 2
- setProperty:
id: setProperty-1014206318
name: fullData
expression:
simple: >-
{"id": "${header.id}", "json": ${exchangeProperty.customerData},
"xml": ${exchangeProperty.orderData}}
-
name
: name of the property to be created or updated in the exchange context. In this case, the property is namedfullData
. -
expression
: defines the property value using an expression. Here, the simple language is used.-
${header.id}
: extracts the value of theid
header and assigns it to theid
key. -
${exchangeProperty.customerData}
: retrieves the value stored in thecustomerData
exchange property and assigns it to thejson
key. -
${exchangeProperty.orderData}
: retrieves the value stored in theorderData
exchange property and assigns it to thexml
key.
-
-
The expression creates a consolidated JSON object combining information from different parts of the message, such as:
{ "id": "12345", "json": {"name": "John Doe", "age": 30}, "xml": "<order><id>123</id><total>100.00</total></order>" }
The use of ">-" at the beginning of the expression maintains YAML readability by treating the processed value as a single line without unnecessary breaks. |
Share your suggestions with us!
Click here and then [+ Submit idea]