SlideShare a Scribd company logo
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:backuptest" 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:backuptestnewfolder" 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;
}
@Override
public int getPriorityWeighting() {
return 0;
}
@Override
public 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:backuptestnewfolder with file name jsonFile.txt
Now if we open the file jsonFile.txt from location
E:backuptestnewfolder 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
Converting with custom transformer
Ad

More Related Content

What's hot (9)

Mule parsing with json part2
Mule parsing with json part2Mule parsing with json part2
Mule parsing with json part2
Anirban Sen Chowdhary
 
Mule esb object_to_jackson_json
Mule esb object_to_jackson_jsonMule esb object_to_jackson_json
Mule esb object_to_jackson_json
Davide Rapacciuolo
 
Mule esb object_to_json
Mule esb object_to_jsonMule esb object_to_json
Mule esb object_to_json
Davide Rapacciuolo
 
Muleesbobjecttojson
Muleesbobjecttojson Muleesbobjecttojson
Muleesbobjecttojson
Davide Rapacciuolo
 
Mule esb object_to_json
Mule esb object_to_jsonMule esb object_to_json
Mule esb object_to_json
Davide Rapacciuolo
 
Mule esb json_to_object
Mule esb json_to_objectMule esb json_to_object
Mule esb json_to_object
Davide Rapacciuolo
 
Web Services
Web ServicesWeb Services
Web Services
Victor Montalvão
 
Mapping and listing in mule
Mapping and listing in muleMapping and listing in mule
Mapping and listing in mule
Khan625
 
Mapping and listing with mule
Mapping and listing with muleMapping and listing with mule
Mapping and listing with mule
Praneethchampion
 

Viewers also liked (18)

Mule technology
Mule technologyMule technology
Mule technology
F K
 
Web Services
Web ServicesWeb Services
Web Services
F K
 
Mule real-world-old
Mule real-world-oldMule real-world-old
Mule real-world-old
F K
 
Java Programming
Java ProgrammingJava Programming
Java Programming
F K
 
Mule
MuleMule
Mule
F K
 
Cache for community edition
Cache for community editionCache for community edition
Cache for community edition
F K
 
Mule with drools
Mule with droolsMule with drools
Mule with drools
F K
 
Jax ws
Jax wsJax ws
Jax ws
F K
 
Webservices using salesforce
Webservices using salesforceWebservices using salesforce
Webservices using salesforce
F K
 
Mule esb made system integration easy
Mule esb made system integration easy Mule esb made system integration easy
Mule esb made system integration easy
F K
 
Install sonarqube plugin in anypoint
Install sonarqube plugin in anypoint Install sonarqube plugin in anypoint
Install sonarqube plugin in anypoint
F K
 
For each component
For each component For each component
For each component
F K
 
Database component
Database component Database component
Database component
F K
 
Github plugin setup in anypoint studio
Github plugin setup in anypoint studio Github plugin setup in anypoint studio
Github plugin setup in anypoint studio
F K
 
Until successful component in mule
Until successful component in muleUntil successful component in mule
Until successful component in mule
F K
 
Java For Begineers
Java For BegineersJava For Begineers
Java For Begineers
F K
 
WebServices introduction in Mule
WebServices introduction in MuleWebServices introduction in Mule
WebServices introduction in Mule
F K
 
Quartz component
Quartz componentQuartz component
Quartz component
F K
 
Mule technology
Mule technologyMule technology
Mule technology
F K
 
Web Services
Web ServicesWeb Services
Web Services
F K
 
Mule real-world-old
Mule real-world-oldMule real-world-old
Mule real-world-old
F K
 
Java Programming
Java ProgrammingJava Programming
Java Programming
F K
 
Mule
MuleMule
Mule
F K
 
Cache for community edition
Cache for community editionCache for community edition
Cache for community edition
F K
 
Mule with drools
Mule with droolsMule with drools
Mule with drools
F K
 
Jax ws
Jax wsJax ws
Jax ws
F K
 
Webservices using salesforce
Webservices using salesforceWebservices using salesforce
Webservices using salesforce
F K
 
Mule esb made system integration easy
Mule esb made system integration easy Mule esb made system integration easy
Mule esb made system integration easy
F K
 
Install sonarqube plugin in anypoint
Install sonarqube plugin in anypoint Install sonarqube plugin in anypoint
Install sonarqube plugin in anypoint
F K
 
For each component
For each component For each component
For each component
F K
 
Database component
Database component Database component
Database component
F K
 
Github plugin setup in anypoint studio
Github plugin setup in anypoint studio Github plugin setup in anypoint studio
Github plugin setup in anypoint studio
F K
 
Until successful component in mule
Until successful component in muleUntil successful component in mule
Until successful component in mule
F K
 
Java For Begineers
Java For BegineersJava For Begineers
Java For Begineers
F K
 
WebServices introduction in Mule
WebServices introduction in MuleWebServices introduction in Mule
WebServices introduction in Mule
F K
 
Quartz component
Quartz componentQuartz component
Quartz component
F K
 
Ad

Similar to Converting with custom transformer (20)

Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformer
Hasan Syed
 
Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformer
Sunil Komarapu
 
Xml to xml transformation
Xml to xml transformationXml to xml transformation
Xml to xml transformation
Son Nguyen
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mule
mdfkhan625
 
Using XSLT in Mule
Using XSLT in MuleUsing XSLT in Mule
Using XSLT in Mule
Anirban Sen Chowdhary
 
Transformation jsontojsonesb
Transformation jsontojsonesbTransformation jsontojsonesb
Transformation jsontojsonesb
Germano Barba
 
Json to json transformation in mule
Json to json transformation in muleJson to json transformation in mule
Json to json transformation in mule
Antonio Pellegrino
 
Transformation jsontojsonesb
Transformation jsontojsonesbTransformation jsontojsonesb
Transformation jsontojsonesb
Antonio Pellegrino
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mule
Rajkattamuri
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mule
Mohammed625
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mule
javeed_mhd
 
Xslt with mule
Xslt with muleXslt with mule
Xslt with mule
Son Nguyen
 
Xslt in mule
Xslt in muleXslt in mule
Xslt in mule
Hasan Syed
 
Xslt in mule
Xslt in muleXslt in mule
Xslt in mule
Mohammed246
 
Xslt in mule
Xslt in muleXslt in mule
Xslt in mule
Sunil Komarapu
 
Xslt in mule
Xslt in muleXslt in mule
Xslt in mule
Shahid Shaik
 
Xml transform
Xml transformXml transform
Xml transform
Son Nguyen
 
Using xslt in mule
Using xslt in mule Using xslt in mule
Using xslt in mule
Rajkattamuri
 
Using xslt in mule
Using  xslt in muleUsing  xslt in mule
Using xslt in mule
Khan625
 
Using xslt in mule
Using xslt in mule Using xslt in mule
Using xslt in mule
AbdulImrankhan7
 
Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformer
Hasan Syed
 
Converting with custom transformer
Converting with custom transformerConverting with custom transformer
Converting with custom transformer
Sunil Komarapu
 
Xml to xml transformation
Xml to xml transformationXml to xml transformation
Xml to xml transformation
Son Nguyen
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mule
mdfkhan625
 
Transformation jsontojsonesb
Transformation jsontojsonesbTransformation jsontojsonesb
Transformation jsontojsonesb
Germano Barba
 
Json to json transformation in mule
Json to json transformation in muleJson to json transformation in mule
Json to json transformation in mule
Antonio Pellegrino
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mule
Rajkattamuri
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mule
Mohammed625
 
Xml to xml transformation in mule
Xml to xml transformation in muleXml to xml transformation in mule
Xml to xml transformation in mule
javeed_mhd
 
Xslt with mule
Xslt with muleXslt with mule
Xslt with mule
Son Nguyen
 
Using xslt in mule
Using xslt in mule Using xslt in mule
Using xslt in mule
Rajkattamuri
 
Using xslt in mule
Using  xslt in muleUsing  xslt in mule
Using xslt in mule
Khan625
 
Ad

More from F K (17)

Testing soapui
Testing soapuiTesting soapui
Testing soapui
F K
 
Vm component
Vm componentVm component
Vm component
F K
 
Mule management console installation
Mule management console installation Mule management console installation
Mule management console installation
F K
 
Message properties component
Message properties componentMessage properties component
Message properties component
F K
 
Junit in mule
Junit in muleJunit in mule
Junit in mule
F K
 
Commit a project in svn using svn plugin in anypoint studio
Commit a project in svn using svn plugin in anypoint studioCommit a project in svn using svn plugin in anypoint studio
Commit a project in svn using svn plugin in anypoint studio
F K
 
Filter expression
Filter expression Filter expression
Filter expression
F K
 
File component
File component File component
File component
F K
 
Choice component
Choice component Choice component
Choice component
F K
 
Mule esb Data Weave
Mule esb Data WeaveMule esb Data Weave
Mule esb Data Weave
F K
 
Idempotent filter in Mule
Idempotent filter in MuleIdempotent filter in Mule
Idempotent filter in Mule
F K
 
Creating dynamic json in Mule
Creating dynamic json in MuleCreating dynamic json in Mule
Creating dynamic json in Mule
F K
 
Caching and invalidating with managed store
Caching and invalidating with managed storeCaching and invalidating with managed store
Caching and invalidating with managed store
F K
 
Automatic documentation with mule
Automatic documentation with muleAutomatic documentation with mule
Automatic documentation with mule
F K
 
Simple webservice with vm
Simple webservice with vmSimple webservice with vm
Simple webservice with vm
F K
 
mule salesforce
mule salesforcemule salesforce
mule salesforce
F K
 
Multithreading
MultithreadingMultithreading
Multithreading
F K
 
Testing soapui
Testing soapuiTesting soapui
Testing soapui
F K
 
Vm component
Vm componentVm component
Vm component
F K
 
Mule management console installation
Mule management console installation Mule management console installation
Mule management console installation
F K
 
Message properties component
Message properties componentMessage properties component
Message properties component
F K
 
Junit in mule
Junit in muleJunit in mule
Junit in mule
F K
 
Commit a project in svn using svn plugin in anypoint studio
Commit a project in svn using svn plugin in anypoint studioCommit a project in svn using svn plugin in anypoint studio
Commit a project in svn using svn plugin in anypoint studio
F K
 
Filter expression
Filter expression Filter expression
Filter expression
F K
 
File component
File component File component
File component
F K
 
Choice component
Choice component Choice component
Choice component
F K
 
Mule esb Data Weave
Mule esb Data WeaveMule esb Data Weave
Mule esb Data Weave
F K
 
Idempotent filter in Mule
Idempotent filter in MuleIdempotent filter in Mule
Idempotent filter in Mule
F K
 
Creating dynamic json in Mule
Creating dynamic json in MuleCreating dynamic json in Mule
Creating dynamic json in Mule
F K
 
Caching and invalidating with managed store
Caching and invalidating with managed storeCaching and invalidating with managed store
Caching and invalidating with managed store
F K
 
Automatic documentation with mule
Automatic documentation with muleAutomatic documentation with mule
Automatic documentation with mule
F K
 
Simple webservice with vm
Simple webservice with vmSimple webservice with vm
Simple webservice with vm
F K
 
mule salesforce
mule salesforcemule salesforce
mule salesforce
F K
 
Multithreading
MultithreadingMultithreading
Multithreading
F K
 

Recently uploaded (20)

Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
ICT Frame Magazine Pvt. Ltd.
 
Developing Product-Behavior Fit: UX Research in Product Development by Krysta...
Developing Product-Behavior Fit: UX Research in Product Development by Krysta...Developing Product-Behavior Fit: UX Research in Product Development by Krysta...
Developing Product-Behavior Fit: UX Research in Product Development by Krysta...
UXPA Boston
 
Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Middle East and Africa Cybersecurity Market Trends and Growth Analysis Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Preeti Jha
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Understanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdfUnderstanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdf
Fulcrum Concepts, LLC
 
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
SOFTTECHHUB
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
DNF 2.0 Implementations Challenges in Nepal
DNF 2.0 Implementations Challenges in NepalDNF 2.0 Implementations Challenges in Nepal
DNF 2.0 Implementations Challenges in Nepal
ICT Frame Magazine Pvt. Ltd.
 
How Top Companies Benefit from Outsourcing
How Top Companies Benefit from OutsourcingHow Top Companies Benefit from Outsourcing
How Top Companies Benefit from Outsourcing
Nascenture
 
Building Connected Agents: An Overview of Google's ADK and A2A Protocol
Building Connected Agents:  An Overview of Google's ADK and A2A ProtocolBuilding Connected Agents:  An Overview of Google's ADK and A2A Protocol
Building Connected Agents: An Overview of Google's ADK and A2A Protocol
Suresh Peiris
 
React Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for SuccessReact Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for Success
Amelia Swank
 
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More MachinesRefactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Leon Anavi
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Alan Dix
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptxUiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
anabulhac
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
ICT Frame Magazine Pvt. Ltd.
 
Developing Product-Behavior Fit: UX Research in Product Development by Krysta...
Developing Product-Behavior Fit: UX Research in Product Development by Krysta...Developing Product-Behavior Fit: UX Research in Product Development by Krysta...
Developing Product-Behavior Fit: UX Research in Product Development by Krysta...
UXPA Boston
 
Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Middle East and Africa Cybersecurity Market Trends and Growth Analysis Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Preeti Jha
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Understanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdfUnderstanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdf
Fulcrum Concepts, LLC
 
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
SOFTTECHHUB
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
How Top Companies Benefit from Outsourcing
How Top Companies Benefit from OutsourcingHow Top Companies Benefit from Outsourcing
How Top Companies Benefit from Outsourcing
Nascenture
 
Building Connected Agents: An Overview of Google's ADK and A2A Protocol
Building Connected Agents:  An Overview of Google's ADK and A2A ProtocolBuilding Connected Agents:  An Overview of Google's ADK and A2A Protocol
Building Connected Agents: An Overview of Google's ADK and A2A Protocol
Suresh Peiris
 
React Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for SuccessReact Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for Success
Amelia Swank
 
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More MachinesRefactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Leon Anavi
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Alan Dix
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptxUiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
anabulhac
 

Converting with custom transformer

  • 2. 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
  • 3. 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
  • 4. But how can we use Custom transformer to transform in Mule?? .
  • 5. Here I will show you how ……
  • 6. 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.
  • 7. Now, let’s check the code for this flow :- <flow name="CustomXMLToJSONTransformer" doc:name="CustomXMLToJSONTransformer"> <file:inbound-endpoint path="E:backuptest" 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:backuptestnewfolder" 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
  • 8. 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
  • 9. 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; } @Override public int getPriorityWeighting() { return 0; } @Override public void setPriorityWeighting(int weighting) { } }
  • 10. 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:backuptestnewfolder with file name jsonFile.txt
  • 11. Now if we open the file jsonFile.txt from location E:backuptestnewfolder 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
  • 12. Hope you enjoyed the simple yet an amazing trick in Mule
  翻译: