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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • The Ultimate Guide to Code Formatting: Prettier vs ESLint vs Biome
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Navigating Double and Triple Extortion Tactics
  • Designing for Sustainability: The Rise of Green Software
  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 user avatar
Srinu Prasad A
·
Mar. 29, 17 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
10.8K Views

Join the DZone community and get the full member experience.

Join For Free

@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
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!