Mule Java Transformer

Mule allows to use the custom java tranformer in which you can write your own loigic to process the message. Message is passed to this object and this object returns the Message after having the execution completed. The return Object of this tranformer bacome the message for the next component.

package demo2;


import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;

public class MyTrans extends AbstractMessageTransformer{

	@Override
	public String transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
		try {
			return "Tranformed Message :"+message.getPayloadAsString().toUpperCase();
		} catch (Exception e) {
			e.printStackTrace(System.out);
			return null;
		}
	}
}

Flow configuration xml

<?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="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="demo2Flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/demo1" allowedMethods="POST" doc:name="HTTP"/>
        <custom-transformer returnClass="java.lang.String" class="demo2.MyTrans" doc:name="Java"/>
        <set-payload value="Welcome #[payload]" doc:name="Set Payload"/>
    </flow>
</mule>

mule custom java transformer

Postman Request

mule custom java transformer postman request

Tags