converting with custom transformer

13
Transforming with Custom Transformer in Mule

Upload: mdfkhan625

Post on 20-Feb-2017

289 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Converting with custom transformer

Transforming with Custom Transformer in Mule

Page 2: Converting with custom transformer

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

Page 3: Converting with custom transformer

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

Page 4: Converting with custom transformer

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

.

Page 5: Converting with custom transformer

Here I will show you how ……

Page 6: Converting with custom transformer

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.

Page 7: Converting with custom transformer

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

Page 8: Converting with custom transformer

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

Page 9: Converting with custom transformer

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) {}}

Page 10: Converting with custom transformer

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

Page 11: Converting with custom transformer

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

Page 12: Converting with custom transformer

Hope you enjoyed the simple yet an amazing trick in Mule

Page 13: Converting with custom transformer

Thank You