Call Java Methods with DataWeave

Author: Neelam Deshmukh

1. Overview

As Mule is being built on top of Java and Spring, there is a strong integration capability in Mule to invoke Java methods. There are numerous ways to invoke a Java method in Mule.

From a DataWeave statement, you can call Java methods from any Java class that’s in your Mule project.

In this blog, we will learn an approach by using Java Component to call custom java class:

  1. Invokes a static method on a provided instance
  2. Instantiate Java class
  3. Invoke a non static method

2. Invoke a Java static method from a class

Step 1: Create a class as part of a Java package named “utils” in your Mule project’s src/main/java folder.

Step 2: Create a mule flow to call the method appendRandom() from Dataweave code

Step 3: Dataweave script to call appendRandom()

Step 4: Postman Console

3. Instantiate Java class

Step 1: Create a class as part of a Java package named “utils” in your Mule project’s src/main/java folder.

Step 2: Create a mule flow to call the method appendRandom() from Dataweave code

Step 3: Dataweave script to call appendRandom()

Step 4: Postman Console

4. Invoke a Java non static method from a class

Step 1: Create a class as part of a Java package named “utils” in your Mule project’s src/main/java folder.

Step 2: Create a mule flow to call a non-static function. We need to first create an instance of the class by using the “New” and “Invoke” java component from the mule palette.

Step 3: New and Invoke Java Components Configuration

Step 4: Postman Console

5. Conclusion

In this tutorial, we looked at approaches to call Java components in Mule 4. 

6. References

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.