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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Document Generation API: How to Automate Personalized Document Creation at Scale
  • Reproducibility as a Competitive Edge: Why Minimal Config Beats Complex Install Scripts
  • Building a Card Layout Using CSS Subgrid
  • How To Build AI-Powered Prompt Templates Using the Salesforce Prompt Builder

Trending

  • The Retry Budget Pattern: How to Stop Retry Storms in API-Led and Microservice Systems
  • Building an Async Validation API With AWS Bedrock Agents and Serverless Architecture
  • Database Bottlenecks Nobody Talks About: Optimizing SQL Queries Beyond Indexing
  • Securing Loop Engineering: Six Trust Boundaries for Autonomous Agents

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.

By 
Ankit Lawaniya user avatar
Ankit Lawaniya
·
Aug. 12, 17 · Tutorial
Likes (13)
Comment
Save
Tweet
Share
22.8K 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.

Related

  • Document Generation API: How to Automate Personalized Document Creation at Scale
  • Reproducibility as a Competitive Edge: Why Minimal Config Beats Complex Install Scripts
  • Building a Card Layout Using CSS Subgrid
  • How To Build AI-Powered Prompt Templates Using the Salesforce Prompt Builder

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook