Over a million developers have joined DZone.

DataWeave - Tip #3

DZone's Guide to

DataWeave - Tip #3

For today's DataWeave tip, we check out how we can use DataWeave to use message processors. Read on to get an expert's tips!

· Integration Zone ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

I have already presented how to call Java code using message processors with the newest Java Module for Mule 4.x. For earlier Mule versions, Entry Point Resolvers were used to invoke custom Java code. However, for scenarios when we would like to use custom code in DataWeave, for transformations, another approach is needed. The approach presented in this article will be more concise, comparing ways in which we can use message processors. This process has been highly extended, compared to the possibilities of Mule 3.x. Mule 3 allowed us to invoke static methods. In contrast, Mule 4 not only permits us to call static methods but also instantiate classes and access their instance attributes.

Invoke a Method

Below, I have attached the DataWeave code responsible for converting an array into a list. To perform the conversion, I will use the already existing asList static method in the Arrays class.

%dw 2.0
import asList from java!java::util::Arrays
output application/java
asList(["Santa Clarita Diet", "Grimm", "Harry Potter", "Under the dome", "Penny Dreadful"])

To perform the call on any class, first, we need to import it. In order to do this, the import keyword is used.

As you can see, we need to import from the java! statement. After that, we provide the full path to the class that we are importing. In contrast to the Java way, two colons are used as a separator. The last part must be the class name. We are not able to perform an import of the whole package. Only the class is permitted.

In our example, I have imported the Arrays class from the java.util package. During import, I was more specific and I chose to import only the asList method. If I did not specify anything between imports and from keywords, all methods would be available.

The asList method accepts an array of any type. As you can see, DataWeave nicely converted it to a Java array. Some of you may be aware that asList also accepts a list of parameters that are treated as an array - varargsHowever, this feature is not supported.

Below, I have imported all the methods from the Collections class.

%dw 2.0
import java!com::profitonilne::utils::Collections
output application/java
Collections::intersection(vars.firstList, vars.secondList)

At line 5, you can see that in order to use the static interasection() method, I added it before the class name and separated it with double colons.

Create an Instance

Creating an instance is a very simple process, and is similar to invoking a method. I have prepared the Item class with two constructors. Notice that we have one private attribute called value and one public method, toUpper().

package com.profitonilne.utils;

import java.nio.charset.Charset;
import java.util.Random;

public class Item {

  private String value;

  public Item() {
    byte[] array = new byte[7]; // length is bounded by 7
    new Random().nextBytes(array);
    String generatedString = new String(array, Charset.forName("UTF-8"));

    this.value = generatedString;

   public Item(String value) {
   this.value = value;

   public void toUpper() {
     this.value = value.toUpperCase();

In order to instantiate it and get thevariable's value in Java I would code something like:

  • new Item("How I met your mother").value

Below, as you can see, I used the class name Item and called the constructor using the ::new construct. After that, value is returned. Please notice that in order to get an attribute I used a dot instead of double colons.

%dw 2.0
import java!com::profitonilne::utils::Item
output application/json
payload ++ [Item::new("How I met your mother").value] 

You need to be aware of the fact that we are able to instantiate a class and get its attribute, but we are not able to call any non-static method at all. We would not be able to do something like the following:

  • Item::new("How I met your mother")::toUpper()


Importing and using Java code within DataWeave is straightforward. You can import all the methods or only the interesting ones using the import keyword in the header section. DataWeave, behind the scenes, performs all the conversions to Java constructs that are required by the method's parameters. In comparison to using a Java Module, it may be simplified even more, as you do not need to add your class manually to mule-artifact.json file. I found it very interesting that the DataWeave engine was able to enter private variables without any setters and getters and return it. It is a pity that we are not able to chain calls of methods and instance methods. However, I rate this feature high, as it simplifies Java calls in DataWeave.

Build and deploy API integrations 7x faster. Try the Cloud Elements 100% RESTful platform for 30 days free. Access your trial here.

integration ,dataweave ,mule

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}