DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Step-by-Step Guide to Use Anypoint MQ: Part 1
  • Spring Transaction Propagation in a Nutshell
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice

Trending

  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • The Smart Way to Talk to Your Database: Why Hybrid API + NL2SQL Wins
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  1. DZone
  2. Coding
  3. Java
  4. Invoke Java Functions in Your DataWeave Transformation and Mule Flow

Invoke Java Functions in Your DataWeave Transformation and Mule Flow

DataWeave is a language used to query and transform complex data. It is very much so possible to invoke Java/Groovy functions within DataWeave.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Mar. 12, 17 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
26.4K Views

Join the DZone community and get the full member experience.

Join For Free

DataWeave makes transformations very easy. DataWeave is a language used to query and transform complex data. It contains a lot of operators, including filters and functions. It supports various formats including XML, JSON, CSV, Java, and EDI out of the box. DataWeave code looks like JSON syntax and is format-neutral. It is very much so possible to invoke Java/Groovy functions within DataWeave.

Invoke MEL Function Within DataWeave Transformation

You will receive two numbers as the input message (JSON format) and return an addition of two numbers. Below you can see what will be the input and output:

Input:

{
  "num1":1,
  "num":2
}

Output:

{
  "sum":3
}

Declare Spring MEL Function

You need declare a Spring MEL function in the global configuration XML as shown below:

<configuration doc:name="Configuration">
	<expression-language>
		<global-functions>
    def toStringLowerCase(name) {
    return name.toLowerCase()
    }
    def addition(a,b) {
    return a + b
    }
   </global-functions>
	</expression-language>
</configuration>

DataWeave Transform

You need to invoke the function addition expecting two arguments. It invokes the DataWeave transform as shown below:

%dw 1.0
%output application/json
---
{
sum: addition(payload.num1,payload.num2)
}

Image title

Invoke Java Function Within DataWeave Transform

Now, you will see how to invoke Java functions with your DataWeave transformation. For example, if you have a class with name TestClass.java that has function addTwoNumbers, accept two arguments and return the sum of two arguments.

package javamuleflow;

public class TestClass {

 public int addTwoNumbers(int a, int b) {
  return a + b;
 }
}

Mule Global Configuration

Now, you need to make the entry of Java function in Mule Global Configuration as shown below:

<configuration doc:name="Configuration">
	<expression-language>
		<import class="javamuleflow.TestClass" />
		<global-functions>
    def addition(a,b) {
    return javamuleflow.TestClass.addTwoNumbers(a,b);
    }
   </global-functions>
	</expression-language>
</configuration>

DataWeave Transform

You need to invoke the function addition expecting two arguments. It invokes the DataWeave transform as shown below:

%dw 1.0
%output application/json
---
{
sum: addition(payload.num1,payload.num2)
}

Invoke Java Function Within Mule Flow

Now, you will see how to invoke the Java function with a Mule flow.

Define Spring Beans

You need to define Spring beans in your Mule XML flow as shown below:

<spring:beans>
	<spring:bean name="invokeJavaFunc" class="javamuleflow.TestClass"></spring:bean>
</spring:beans>

name is just the name of the object in the class and  class is the fully qualified name of the Java class.

Invoke Component

Add an invoke component in your flow. This will invoke your Java class in the flow.Image titleDisplay Name is a unique name for your component in the application.

  • Name is the name of message processor used for logging purposes.

  • Object Ref is a bean name. It references an object that contains the method to be invoked.

  • Method is a function that needs to be invoked.

  • Method arguments is the parameter that needs to be passed to the function. Multiple arguments can be specified using commas.

  • Method arguments types is the data type for the argument. Multiple argument types can be specified using commas. Use this property whenever you have multiple methods with the same name in same the class.

Now, you know how to invoke Java functions within your DataWeave transformation and Mule flow!

Java (programming language) Flow (web browser)

Opinions expressed by DZone contributors are their own.

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Step-by-Step Guide to Use Anypoint MQ: Part 1
  • Spring Transaction Propagation in a Nutshell
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!