Integration with Choice
Flow script
Consider the script of the flow below.
Also refer to the summary and the description of the elements for better understanding.
- from:
uri: rest:post:/demo
steps:
- choice:
otherwise:
steps:
- setBody:
expression:
constant:
expression: "Error: ID not found in the request body"
- setHeader:
expression:
constant:
expression: "400"
name: CamelHttpResponseCode
when:
steps:
- setBody:
expression:
constant:
expression: Success
- setHeader:
expression:
constant:
expression: "200"
name: CamelHttpResponseCode
expression:
jsonpath:
expression: $.model.Details[?(@['Random nonsense'] == 'New today')]
Summary
This flow sets up a route that responds to POST requests at the /demo
endpoint.
Depending on the content of the request, the flow performs a conditional check:
The condition checks if Random nonsense
within $.model.Details
is New today
.
-
If the condition is true:
-
The response body is set to
Success
. -
The
CamelHttpResponseCode
header is set to200
.
-
-
If the condition is false:
-
The response body is set to
Error: ID not found in the request body
. -
The
CamelHttpResponseCode
header is set to400
.
-
The choice
structure allows for flexible conditional routing, while setBody
and setHeader
allow dynamically setting the response body and headers based on the evaluated conditions.
Description of the elements
Below is the explanation of each element of the integration script:
-
from
Defines the entry point of the Camel route. Specifies that the route responds to HTTP POST requests at the
/demo
endpoint.- from: uri: rest:post:/demo
-
steps
Lists the actions to be executed when a request is received at the defined endpoint. In the example, it contains a conditional
choice
structure.steps: - choice:
-
choice
Defines a conditional structure that can have multiple paths, executing different sets of actions depending on the specified conditions.
choice:
-
otherwise
Defines the set of actions to be executed if none of the when conditions are met. In this case, it sets the response body to an error message and the HTTP response code to
400
(Bad Request).otherwise: steps: - setBody: expression: constant: expression: "Error: ID not found in the request body" - setHeader: expression: constant: expression: "400" name: CamelHttpResponseCode
-
setBody (otherwise)
Defines the HTTP response body when the otherwise condition is met. Here, the body is set to the constant string
Error: ID not found in the request body
. [1]- setBody: expression: constant: expression: "Error: ID not found in the request body"
-
setHeader (otherwise)
Defines a specific HTTP header for the response when the otherwise condition is met. Here, the
CamelHttpResponseCode
header is set to400
.- setHeader: expression: constant: expression: "400" name: CamelHttpResponseCode
-
when
Defines a set of actions to be executed if a specific condition is met. In this case, the condition checks if the
Random nonsense
field within$.model.Details
is equal toNew today
.when: steps: - setBody: expression: constant: expression: Success - setHeader: expression: constant: expression: "200" name: CamelHttpResponseCode expression: jsonpath: expression: $.model.Details[?(@['Random nonsense'] == 'New today')]
-
steps (when)
Lists the actions to be executed if the when condition is met. In this case, it sets the response body to
Success
and the HTTP response code to200
(OK).steps: - setBody: expression: constant: expression: Success - setHeader: expression: constant: expression: "200" name: CamelHttpResponseCode expression: jsonpath: expression: $.model.Details[?(@['Random nonsense'] == 'New today')]
-
setBody (when)
Defines the HTTP response body if the when condition is met. Here, the body is set to the constant string
Success
.- setBody: expression: constant: expression: Success
-
setHeader (when)
Defines a specific HTTP header for the response if the when condition is met. Here, the
CamelHttpResponseCode
header is set to200
.- setHeader: expression: constant: expression: "200" name: CamelHttpResponseCode
-
expression (when)
Specifies the condition to be evaluated to determine if the when block should be executed. Uses a JSONpath expression to check if the
Random nonsense
field within$.model.Details
is equal toNew today
.expression: jsonpath: expression: $.model.Details[?(@['Random nonsense'] == 'New today')]
Share your suggestions with us!
Click here and then [+ Submit idea]