Calling Global MEL Functions from Dataweave Code in Mule
If you define a global Mule Expression Language (MEL) function in your Mule project, you can then invoke it anywhere in your Dataweave code, without need for any special syntax.
To create one such global function, you must edit your Mule project’s XML file and enclose any functions that you wish to define in the following set of tags, which must be placed in the global elements section, before any of the flows are defined.
flow of calling global MEL Function :
Syntax for global MEL function :
<configuration doc:name="Configuration">
<expression-language>
<global-functions>
</global-functions>
</expression-language>
</configuration>
Transform message configuration :
URL : localhost:8085/api/dataweaveflow
Method : GET
Output :
{
"person": {
"name": "Himanshu"
},
"employee": "HIMANSHU"
}
XML project code :
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8085" basePath="/api" doc:name="HTTP Listener Configuration"/>
<configuration doc:name="Configuration">
<expression-language>
<global-functions>
def newUser() {
return ["name" : "Himanshu"]
}
def upperName(user) {
return user.name.toUpperCase()
}
</global-functions>
</expression-language>
</configuration>
<flow name="test_dataweaveFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/dataweaveflow" doc:name="HTTP"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
"person" : newUser(),
"employee": upperName(newUser())
}]]></dw:set-payload>
</dw:transform-message>
</flow>
</mule>