DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Parse Template Transformer in Mule

Learn how and when to use the Parse Template component in Mule and see an example of a request and response in a Mule flow.

Ankit Lawaniya user avatar by
Ankit Lawaniya
CORE ·
Aug. 12, 17 · Tutorial
Like (13)
Save
Tweet
Share
19.89K Views

Join the DZone community and get the full member experience.

Join For Free

The Parse Template component loads a file into the Mule payload. The 'Parse Template' transformer parses a template file that can contain MEL expressions and places the resulting string into the message payload.We can use the payload,flowVars and inboundproperties in the template to create the dynamic content.

  • Use Case 1: When we want to prepare the payload to be used by the Web Service Consumer or for a SOAP request.

  • Use Case 2: When we want to create a template for emailing.

  • Use Case 3: There are other scenarios where it can be useful, such as returning HTML to a client from an HTTP transport. We can load an HTML file into the payload that includes embedded MEL.  

Samples of MEL that we can define in the template:

  • #[message.inboundProperties.'http.query.params'.name]

  • #[flowVars.accountNumber]

  • #[payload[0]['order_no']]

Let’s walk through how to use JSON Schema validator in Mule application. In this example, we are receiving the order through HTTP call from the end user. We are using a parse template to load a file from an external location – a file which, behaving as a template, expects values for "order number," "order description," and "order provisioning date." The parse template extracts info from the Mule message variables to insert as values into the template and set the resulting contents as the message payload.

Mule Flow:Image title

Code:

<?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:jms="http://www.mulesoft.org/schema/mule/jms"
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.8.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.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/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd 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:listener-config name="HTTP_Listener_Configuration"
host="localhost" port="8081" basePath="/parse" doc:name="HTTP Listener Configuration" />
<flow name="parseTempate">
<http:listener config-ref="HTTP_Listener_Configuration" path="/parse"/> 
<set-payload mimeType="application/json" value="#[payload]" />
<json:json-to-object-transformer
returnClass="java.lang.Object" doc:name="JSON to Object" />
<parse-template location="responseHtml.template"
doc:name="Parse Template" />
<set-property doc:name="Content Type" propertyName="Content-Type"
value="text/html" />
</flow>
</mule>

responseHtml.template

<html>
        <body>
                <table border=4>
                <tr>
                        <th>Order No</th>
                        <th>Order Description</th>
                         <th>Provisioning Date</th>  
                </tr>
                <tr>
                        <td>#[payload[0]['order_no']]</td>
                        <td>#[payload[0]['order_description']]</td>
                         <td>#[payload[0]['provisioning_date']]</td>
                </tr>
                </table>
        </body>
</html>

To execute this flow, we will have to open the REST client and pass the payload as part of the POST request body. Below are the screenshots of the request and response.

Request:

[
{ 
"order_no" : "199",
"order_description" : "PostpaidOrder",
"provisioning_date" : "28/07/2017"
}
]


Image title

Hope this helps.

Thanks.

Keep learning.

Template

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Web Application Architecture: The Latest Guide
  • The Future of Cloud Engineering Evolves
  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin
  • Kotlin Is More Fun Than Java And This Is a Big Deal

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: