Call Java Methods with DataWeave

DataWeave 2.1 is compatible with Mule 4.1. Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule will reach its End of Life on November 2, 2022, when Extended Support ends.

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.

From a DataWeave statement, you can call Java Methods from any Java class that’s in your Mule project. Note that you can only call Static methods via DataWeave (methods that belong to a Java class, not methods that belong to a specific instance of a class).

Call a Java Method

Before you can call a method, you must first import the class it belongs to into your DataWeave code. You can import Java classes just as you import DataWeave modules by including java! into the statement.

For example, below is a simple Java class with a single method that appends a random number at the end of a string. Assume that you created this class as part of a Java package named org.mycompany.utils in your Mule project’s src/main/java folder.

package org.mycompany.utils;

import java.util.Random;

public class MyUtils {

	public static String appendRandom(String base) {
		return base + new Random().nextInt();
	}

}

You can call the method appendRandom() from DataWeave code in any of the following ways:

  • Import the class and then refer to the method:

    %dw 2.0
    import java!org::mycompany::utils::MyUtils
    output application/json
    ---
    {
    	a: MyUtils::appendRandom("myString")
    }
  • Import one or more methods instead of the whole class:

    %dw 2.0
    import java!org::mycompany::utils::MyUtils::appendRandom
    output application/json
    ---
    {
    	a: MyUtils::appendRandom("myString")
    }
  • If the method is a static method, import and call it in a single line:

    %dw 2.0
    output application/json
    ---
    {
    	a: java!org::mycompany::utils::MyUtils::appendRandom(vars.myString)
    }

All three methods return the variable value with a random string appended:

{
  "a":"myString969858409"
}

Instantiate a Java Class

Through DataWeave code, you can instantiate a new object of any class. Note that after creating an instance, you can’t call its instance methods with DataWeave. However, you can reference its variables.

Below is a simple Java class that has a method and a variable.

package org.mycompany.utils;
public class MyClass {

	private String foo;
	 public MyClass(String foo) {
		 this.foo = foo;
	}

	 public String getFoo() {
		return foo;
	}

}

The DataWeave example below first imports the MyClass class, then creates a new instance of the class and calls its instance variable foo. Note you cannot call the private method getFoo() with DataWeave.

%dw 2.0
import java!org::mycompany::utils::MyClass
output application/json
---
{
	a: MyClass::new("myString").foo
}

The script produces the following output:

{
  "a":"myString"
}

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub