In this example, we will create a simple Mule application to implement the following flow:
The flow accepts HTTP requests, sets a payload on the message, applies some transformation on the payload, and finally returns a response to the end user.
So we will use :
HTTP endpoint
: it allows, in the beginning of the flow, to receive request from the end user, and, in the end of the flow, to return the message as response to the end user.Java Transformer
: to apply a custom transformation on the payload :"Hello " + {payload}
Echo component
: to display the message payload.
1. Technologies used
- Anypoint Studio
- JDK 1.7
2. Mule XML Editor
The following is the flow in XML representation:<?xml version="1.0" encoding="UTF-8"?>
<mule 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" version="EE-3.5.0"
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">
<flow name="mulehelloworldexampleFlow1" doc:name="mulehelloworldexampleFlow1">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="8081" doc:name="HTTP" />
<custom-transformer class="com.keylesson.transformer.KeyTransformer"
doc:name="Key Transformer" />
<echo-component doc:name="Echo" />
</flow>
</mule>
3. Java Transformer
To apply a custom transformation on the message payload, we have used Java Transformer.The following is the class called by the Java Transformer :
package com.keylesson.transformer;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;
public class KeyTransformer extends AbstractTransformer {
@Override
protected Object doTransform(Object src, String enc)
throws TransformerException {
return "Hello " + src.toString().substring(1);
}
}