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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • DataWeave Interview Question: Compare IDs From Two Arrays and Create a New Array
  • Filtering Java Collections via Annotation-Driven Introspection
  • Merge Multiple PDFs in MuleSoft
  • On-Demand-Schedulers With MuleSoft CloudHub APIs

Trending

  • Designing Frontend Applications for Failure, Not Just Success
  • Closing the Drift Loop: An AI Agent That Drafts Terraform Fixes
  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • Delta Lake UniForm to Snowflake: Cross-Platform Table Access Without a Second Copy

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. MuleSoft: Payload Annotation Usage in Java Component

MuleSoft: Payload Annotation Usage in Java Component

Learn about how to retrieve the JSON payload in the Java component using the @Payload annotation, which controls how a message payload is passed.

By 
Srinu Prasad A
·
Mar. 29, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
11.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

@Payload annotation controls how the current message payload is passed into a method by performing automatic transformation of the message payload to match the annotated parameter type. A parameter injection annotation that can be used on component entry points.

The primary goal of this article is to retrieve the JSON payload in the Java component using the@Payload annotation.

I created a Book POJO class under src/main/java.

package com.ss.component;

public class Book {

 private String title;
 private String author;
 private double price;

 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public String getAuthor() {
  return author;
 }
 public void setAuthor(String author) {
  this.author = author;
 }
 public double getPrice() {
  return price;
 }
 public void setPrice(double price) {
  this.price = price;
 }
 @Override
 public String toString() {
  return "Book [title=" + title + ", author=" + author + ", price=" + price + "]";
 }
}

...and a component class as:

package com.ss.component;

import org.mule.api.annotations.param.Payload;

public class MyComponent {
 public Object process(@Payload Book book) {
  System.out.println("Title:" + book.getTitle());
  System.out.println("Author:" + book.getAuthor());
  System.out.println("Price:" + book.getPrice());

  return book;
 }
}

Develop the flow as follows:

<?xml version="1.0" encoding="UTF-8"?>
<mule
	xmlns:json="http://www.mulesoft.org/schema/mule/json"
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
	<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="9001" doc:name="HTTP Listener Configuration"/>
	<flow name="component_annotationFlow">
		<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP" allowedMethods="POST"/>
		<json:json-to-object-transformer returnClass="com.ss.component.Book" doc:name="JSON to Object"/>
		<component doc:name="Java">
			<singleton-object class="com.ss.component.MyComponent"/>
		</component>
		<object-to-string-transformer doc:name="Object to String"/>
		<logger message="#[payload]" level="INFO" doc:name="Logger"/>
	</flow>
</mule>

And here's the flow diagram:Image title

Run and deploy the flow to Anypoint runtime.

Test the flow with the POSTMAN plug-in:

Image title

Hit the HTTP URL with a POST request with a JSON body as:

      {
        "title": "programming in c",
        "author": "bala guru swamy",
        "price": "100"
      }

We can observe output in the console as:

  • Title: programming in c. 

  • Author: bala guru swamy. 

  • Price: 100.

INFO 2017-03-24 16:30:11,640:

[[component_annotation].HTTP_Listener_Configuration.worker .01]
org.mule.api.processor.LoggerMessageProcessor: Book[title = programming in c, author = bala guru swamy, price = 100.0]

Now, we have accessed the JSON payload in a Java component. 

Payload (computing) Java (programming language) Annotation MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • DataWeave Interview Question: Compare IDs From Two Arrays and Create a New Array
  • Filtering Java Collections via Annotation-Driven Introspection
  • Merge Multiple PDFs in MuleSoft
  • On-Demand-Schedulers With MuleSoft CloudHub APIs

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook