13
Transforming with Custom Transformer in Mule

Converting with custom transformer

Embed Size (px)

Citation preview

Transforming with Custom Transformer in Mule

Sometime in our Mule flow we require to transform a payload from one form to another.For example in some cases, we need transform an XML payload to JSON

Now there are several ways of transforming the XML payload to JSON in Mule. You can use XML to Object transformer and then Object to JSON transformer in doing so.But how about using a custom transformer to directly transform XML to JSON ?It will be a very easy way in achieving that without using much transformer in our flow and can directly transform end-to-end

But how can we use Custom transformer to transform in Mule??

.

Here I will show you how ……

Let us consider we have a following Mule flow :-

Now you can see in the above flow the inbound endpoint will pic a file that contains XML content from a location and put it into another location and the outbound file will contain it’s corresponding JSON content.You can also see a custom transformer in the middle which is responsible for this XML payload conversion to JSON directly.

Now, let’s check the code for this flow :-<flow name="CustomXMLToJSONTransformer" doc:name="CustomXMLToJSONTransformer"> <file:inbound-endpoint path="E:\backup\test" responseTimeout="10000" doc:name="File"> <file:filename-regex-filter pattern="xmlFile.txt" caseSensitive="false"/> </file:inbound-endpoint> <file:file-to-string-transformer doc:name="File to String"/><custom-transformer class="CustomXMLToJSONTransformer" doc:name="XmlToJson"/> <logger message="#[message.payload]" level="INFO" doc:name="Logger" /><file:outbound-endpoint path="E:\backup\test\newfolder" outputPattern="jsonFile.txt" responseTimeout="10000" doc:name="File" /></flow>

As you can see the file xmlFile.txt contains XML content that will be converted directly into JSON by the custom-transformer into a file jsonFile.txt

Now let’s check the XML content of file xmlFile.txt :-

So, the above is the XML content we need to convert using our custom transformer into a corresponding JSON

So our custom transformer java class is the following :-public class CustomXMLToJSONTransformer extends AbstractMessageTransformer implements DiscoverableTransformer {

public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {

try { String xml = (String) message.getPayload();

XmlMapper xmlMapper = new XmlMapper(); List entries = xmlMapper.readValue(xml, List.class);

ObjectMapper jsonMapper = new ObjectMapper(); String json = jsonMapper.writeValueAsString(entries); return json; } catch (Exception e) { System.out.println("Error: " + e); e.printStackTrace(); } return null;}

@Overridepublic int getPriorityWeighting() { return 0;}

@Overridepublic void setPriorityWeighting(int weighting) {}}

Now let’s test our application . We will see the following in our Mule console :-

You can see the payload is transformed into JSON and has been dispatched to location E:\backup\test\newfolder with file name jsonFile.txt

Now if we open the file jsonFile.txt from location E:\backup\test\newfolder we will get our JSON content as following

You can see you have generated the JSON for the XML directly just using a simple custom transformer component

Hope you enjoyed the simple yet an amazing trick in Mule

Thank You