Contact Free trial Login

Call Java Methods with DataWeave

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). Before you begin, note that DataWeave 2.0 (%dw 2.0) is for Mule 4 apps. For a Mule 3 app, refer to DataWeave 1.0 (%dw 1.0) examples, within the Mule 3.9 documentation set. For other Mule versions, you can use the Mule Runtime version selector in the table of contents.

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 "utils" in your Mule project’s src/main/java folder.

package 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!utils::MyUtils
    output application/json
    ---
    {
    	a: MyUtils::appendRandom("myString")
    }
  • Import one or more methods instead of the whole class:

    %dw 2.0
    import java!utils::MyUtils::appendRandom
    output application/json
    ---
    {
    	a: 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!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 through DataWeave. However, you can reference its variables.

This simple Java class has a method and a variable.

package utils;
public class MyClass {

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

	 public String getFoo() {
		return foo;
	}

}

To create the MyClass example in a Studio project:

  1. Create a project for the class in Studio by clicking NewMule Project and providing a name, such as my-app, and clicking Finish.

  2. From my-app, create the utils package by right-clicking src/main/java from Studio’s Package Explorer, selecting NewPackage, and naming the package utils.

  3. Create the MyClass class in the your new utils package by right-clicking utils, selecting NewClass, naming that class MyClass, and clicking Finish.

  4. In the MyClass.java tab that opens in Studio, copy, paste, and save the contents MyClass.

The following DataWeave example imports MyClass, creates a new instance of the class, and calls its instance variable foo. (Note that it is not possible for DataWeave to call the object’s getFoo() method.)

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

To add this DataWeave example to your Studio project:

  1. Returning to my-app.xml in src/main/mule, drag a Transform Message component into the project’s canvas, and click to open it.

  2. In the component’s Transform Message tab, replace the entire script in the source code area (on the right) with the DataWeave script (above).

  3. Click Preview (located farthest right on the tab) to view the following output:

    {
      "a":"myString"
    }

Note that the XML for the Transform Message configuration in the Mule flow looks something like this:

<flow name="my-appFlow" >
	<ee:transform doc:name="Transform Message" >
		<ee:message >
			<ee:set-payload ><![CDATA[%dw 2.0
import java!utils::MyClass
output application/json
---
{
a: MyClass::new("myString").foo
}]]></ee:set-payload>
		</ee:message>
	</ee:transform>
</flow>

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.