DZone
Integration Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > 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.

Srinu Prasad A user avatar by
Srinu Prasad A
·
Mar. 29, 17 · Integration Zone · Tutorial
Like (6)
Save
Tweet
9.68K 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.

Popular on DZone

  • 5 Steps to Create a Successful Agile Release Plan
  • API Security Weekly: Issue 165
  • Version Number Anti-Patterns
  • Understanding OAuth 2.0

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo