Introduction to Mule 4: DataWeave Expression Language
In Mule 3, you must learn both the Mule Expression Language (MEL) and DataWeave. MEL forces you to convert your payloads from binary data (such as XML or JSON documents) into Java objects so that you can write expressions that access that data, for example, when routing to a specific location.
In Mule 4, DataWeave is the default expression language. Combined with the built-in streaming capabilities, this change simplifies many common tasks:
Events can be routed based on payload data, without requiring conversion to Java objects.
Binary data can easily be queried from an expression anywhere in your flow, for example, when logging.
Streaming now happens transparently. You no longer need to worry about larger-than-memory data streams or about consuming a stream twice.
DataWeave 2.0 also features many improvements, which are covered in the DataWeave 2.0 section.
At the core, expressions continue to work as before. You can use them to extract data, log data, or make decisions about where to route data. And like MEL, the syntax to access properties in your data is very simple.
<logger message="#[payload]"/> <set-variable variableName="httpResponseStatus" value="#[attributes.statusCode]"/>
Unlike MEL, you can use DataWeave to easily access data anywhere in your flow without concern for transforming it into intermediate objects. Consider this example in Mule 3:
<http:listener path="/hello"/> <!-- receives a JSON HTTP Body --> <json:json-to-object/> <!-- convert to Java objects --> <choice> <!-- route data based on the data --> <when expression="#[payload.customer == 'Acme, Inc']"> <!-- do some logic --> </when> </choice>
In Mule 4, you no longer need to convert JSON to an intermediate format. You can simply access the data directly through expressions without losing the original underlying data, and Mule 4 handles all data streaming and random access transparently for you.
<http:listener path="/hello"/> <!-- receives a JSON HTTP Body --> <choice> <!-- route data based on the data --> <when expression="#[payload.customer == 'Acme, Inc']"> <!-- do some logic --> </when> </choice>