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, the myProperty property is set to FixedValue.

- 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 the orderId header and uses it as the value for the myProperty 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 the id 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 the id 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 of myProperty is calculated by a Groovy script that increments the value of the orderId 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 as itemId 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 named id within a structure like a map (JSON or similar). The value extracted from the id field is assigned to the itemId property.

    For example, if the message body is:

    {
      "id": "12345",
      "name": "Sample Item"
    }
    • The Camel route evaluates the expression ${body['id']} and extracts the value 12345 from the id field, storing it in the property named itemId.

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 named fullData.

  • expression: defines the property value using an expression. Here, the simple language is used.

    • ${header.id}: extracts the value of the id header and assigns it to the id key.

    • ${exchangeProperty.customerData}: retrieves the value stored in the customerData exchange property and assigns it to the json key.

    • ${exchangeProperty.orderData}: retrieves the value stored in the orderData exchange property and assigns it to the xml 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.
Thanks for your feedback!
EDIT

Share your suggestions with us!
Click here and then [+ Submit idea]