2. 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. 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. Now we can use the setPayload component to populate the bean created before:
Value: #[new.com.mulechampion.JsonClass(‘Usern’,’Users)]
As expected, running this app, will produce the following log on console:
HTTP_Listener_Configuration.worker.01]
org.mule.api.processor.LoggerMessageProcessor: JsonClass [name=Usern,
surname=Users]
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":“Usern","surname":“Users"}
Enjoy with the MULE!