Custom Dataweave Function Using Custom Module
In addition to using the built-in DataWeave function you can also create and use custom modules and mapping files. You write modules and mapping files in a DataWeave Language (.dwl) file and import into your Mule app through DataWeave scripts in Mule components. Both modules and mapping files are useful when you need to reuse the same functionality or feature over and over again.
Custom modules can define functions, variables, types, and namespaces. You can import these modules into a DataWeave script to use the features.
Custom mapping files are a type of module that contains a complete DataWeave script that you can import and use in another DataWeave script or reference in a Mule component.
Note: Unlike a typical DataWeave script or mapping file, a custom DataWeave module cannot contain an output directive, body expression, or the separator (—) between header and body sections.
Create a project in anypoint studio, configure the listener and add the modules folder under src/main/resource to keep the .dwl
Create the .dwl and define the custom function
%dw 2.0 fun getUpdatedsting(value:String) = "Welcome" ++" " ++ value
Create the Transform message and invoke the custom function from Dataweave as shown below
Dataweave
%dw 2.0 import modules::customdataweave output application/json --- { "WelcomeMessage": customdataweave::getUpdatedsting("John") }
Deploy the project and test from soap ui
Sample Application: customdataweave sample application
Soap UI Project: Sample soap ui project