Choice and Split: Processing an Order List
Use Case
Let’s consider a scenario where a company wants to process a list of orders received from an external system. Each order has a total value, and the company needs to route orders with a value greater than R$ 10,000 to a specific endpoint for priority processing, while other orders are sent to a standard endpoint.
The integration follows this route:
-
Flow start:
A REST endpoint is exposed to start the order processing flow.
-
Retrieving the order list:
A GET request is made to an endpoint that returns a list of orders in JSON format.
-
Data deserialization:
The JSON response is deserialized using the Jackson library to convert the data into usable objects.
-
Order processing:
The order list is iterated. For each order, it is checked whether the total value exceeds R$ 10,000.
-
Order routing:
Orders with a value greater than R$ 10,000 are sent to a specific endpoint for priority processing. Orders with a value less than or equal to R$ 10,000 are sent to a standard endpoint for regular processing.
Description of the steps
Check out below the description of the steps:
-
Flow start: the flow starts with a
GET
request to the/startflow
endpoint.- from: Description: REST endpoint that triggers the flow uri: rest:get:/startflow
-
Retrieving the order list: then the flow makes another
GET
request to an endpoint that returns a list of items.steps: - toD: uri: https://example.com/api/list parameters: bridgeEndpoint: true httpMethod: GET
-
Data deserialization: the response from the
GET
request is treated as JSON and is deserialized using the Jackson library. This allows the flow to handle the data as objects rather than just plain text.- unmarshal: json: library: Jackson
-
Order processing: the next step splits the JSON response, so that each item can be processed individually. The expression
${body}
refers to the content of the response that was deserialized in the previous step.- split: expression: simple: expression: ${body}
-
Order routing: the Choice defines that for each item in the list:
-
If the
value
field is greater than10,000
, aPOST
is sent tohttps://example.com/api/true-endpoint
.steps: - choice: when: - expression: simple: "${body[value] > 10.000}" steps: - toD: uri: https://example.com/api/true-endpoint parameters: bridgeEndpoint: true httpMethod: POST
-
Otherwise, a
POST
is sent tohttps://example.com/api/false-endpoint
.otherwise: steps: - toD: uri: https://example.com/api/false-endpoint parameters: bridgeEndpoint: true httpMethod: POST
-
Source Code
See below the full script of the integration flow:
- from:
Description: REST endpoint that triggers the flow
uri: rest:get:/startflow # Endpoint that starts the flow
steps:
- toD:
uri: https://example.com/api/list # GET request to retrieve the list of items
parameters:
bridgeEndpoint: true
httpMethod: GET # Defining the HTTP method as GET
- unmarshal:
json:
library: Jackson # Using the Jackson library to deserialize the JSON response
- split:
expression:
simple:
expression: ${body} # Splitting the response to iterate over the items
steps:
- choice:
when:
- expression:
simple: "${body[value] > 10.000}" # Condition to check if the 'value' field is greater than 10,000
steps:
- toD:
uri: https://example.com/api/true-endpoint # Endpoint to be called if the condition is true
parameters:
bridgeEndpoint: true
httpMethod: POST # HTTP POST method
otherwise:
steps:
- toD:
uri: https://example.com/api/false-endpoint # Endpoint to be called if the condition is false
parameters:
bridgeEndpoint: true
httpMethod: POST # HTTP POST method
Advanced parameters for EIPs and components are not yet available in the configuration form of steps in the Diagram tab. To add them, use the Source Code tab. |
Share your suggestions with us!
Click here and then [+ Submit idea]