<flow name="checkAge">
<http:listener path="person" method="POST" config-ref="http" />
<json:json-to-object-transformer />
<choice>
<when expression="#[payload.age > 21]">
<logger message="adult" />
</when>
<otherwise>
<logger message="underage" />
</otherwise>
</choice>
</flow>
Migrating the Choice Router
Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule reached its End of Life on November 2, 2022, when Extended Support ended. Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted. MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements. |
Little changed between Choice router for Mule 3 and Mule 4 except for:
-
DataWeave is now the expression language so the when statements are actually going to use it instead of MEL
-
The
<otherwise>
block is now optional
Expression Language for the When Block
The when expressions need to be changed from MEL to DataWeave. This also mean that you can leverage DataWeave’s ability to interpret any format to avoid unnecessary convertions. For example, suppose a flow in which a JSON document representing a user is posted through HTTP and you want to check if the person is underage:
With Mule 4, you don’t need to worry about JSON or Java formats and just do:
<flow name="checkAge">
<http:listener path="person" method="POST" config-ref="http" />
<choice>
<when expression="#[payload.age > 21]">
<logger message="adult" />
</when>
<otherwise>
<logger message="underage" />
</otherwise>
</choice>
</flow>