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, themyPropertyproperty 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 theorderIdheader and uses it as the value for themyPropertyproperty.
- setProperty:
name: myProperty
expression:
simple: ${header.orderId}
-
xpath: uses XPath queries to extract data from XML documents. For example, the XPath expression/order/idextracts the value of theidfield 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.idextracts the value of theidfield 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 ofmyPropertyis calculated by a Groovy script that increments the value of theorderIdheader 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 asitemIdin 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 namedidwithin a structure like a map (JSON or similar). The value extracted from theidfield is assigned to theitemIdproperty.For example, if the message body is:
{ "id": "12345", "name": "Sample Item" }-
The Camel route evaluates the expression
${body['id']}and extracts the value12345from theidfield, 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 theidheader and assigns it to theidkey. -
${exchangeProperty.customerData}: retrieves the value stored in thecustomerDataexchange property and assigns it to thejsonkey. -
${exchangeProperty.orderData}: retrieves the value stored in theorderDataexchange property and assigns it to thexmlkey.
-
-
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]