The document describes how to convert an object to JSON in Mule ESB in 5 minutes. It involves creating a simple flow with an HTTP listener, defining a Java POJO class called JsonClass with name and surname fields, populating an instance of the class using the setPayload component, and adding an Object-to-JSON transformer to output the JSON representation of the object. The output of running the application is a log showing the populated JsonClass object and the JSON output.
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
Mule esb How to convert from Object to Json in 5 minutes
1. Mule ESB
How to convert from Object to Json in 5 minutes
Gennaro Spagnoli - 2016
2. Gennaro Spagnoli - 2016
In this tutorial we will be creating an easy object to json converter using mule esb.
Let’s begin creating a simple flow with an http listener, in order to trigger the transformation:
Now, let’s create a java pojo class, called User, like this:
3. Gennaro Spagnoli - 2016
package com.mulechampion;
import java.io.Serializable;
public class JsonClass implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5645625751838751914L;
private String name;
private String surname;
public JsonClass(String name, String surname) {
super();
this.name = name;
this.surname = surname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurnme(String surnme) {
this.surname = surnme;
}
@Override
public String toString() {
return "JsonClass [name=" + name + ", surname=" + surname + "]";
}
}
4. Gennaro Spagnoli - 2016
Now we can use the setPayload component to populate the bean created before:
As expected, running this app, will produce the following log on console:
HTTP_Listener_Configuration.worker.01]
org.mule.api.processor.LoggerMessageProcessor: JsonClass [name=Gennaro,
surname=Spagnoli]
5. Now let’s add the Object-to-json transformer in order to obtain the output JSON
If we launch the application, calling it, we will obtain the json output, as wanted!
HTTP_Listener_Configuration.worker.01]
org.mule.api.processor.LoggerMessageProcessor:
{"name":"Gennaro","surname":"Spagnoli"}