Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MuleSoft : Dynamic Content (Parser/Velocity Template)

DZone's Guide to

MuleSoft : Dynamic Content (Parser/Velocity Template)

In this post, we will discuss, and provide examples of, how to use MuleSoft's Velocity Template and its Parse Template component.

· Integration Zone ·
Free Resource

Learn more about how to Prevent Slow or Broken APIs From Affecting Your Bottom Line.

While working with some Spring applications, I came across Velocity Template, which is used to dynamically replace pieces of content in emails.

Some common use cases for the Velocity Template are welcome, registration, and password reset emails, where the developer just needs to replace the user’s email id, name, or reset password token. In this post, we will go through how to use Velocity Template and its magical component, Parse Template.

Velocity Template

Velocity Engine reads templates from given a resource, or LoaderPath. Add the below Velocity engine bean dependency to your code:

<beans:beans>
<beans:bean name=”velocityEngine” class=”org.springframework.ui.velocity.VelocityEngineFactoryBean”>
<beans:property name=”resourceLoaderPath” value=”classpath:templates”/>
</beans:bean>
</beans:beans>

This Velocity Engine will read template files from the resource/templates folder in your application.

Now, we will add a custom Java class which will have a bean property like the above Velocity engine, and another class which will create the template file name.

<custom-transformer class=”emailsendproject.VelocityMessageTransformer” doc:name=”Java”>
<spring:property name=”velocityEngine” ref=”velocityEngine” />
<spring:property name=”templateName” value=”WelcomeEmail.vm” />
</custom-transformer>

Email Template:

Welcome ${firstname} 
 This is send email with velocity template example demo.
Regards,
Amit Ghorpade

In this case, ${firstname } will be replaced by the dynamic value in the custom Java class using the Velocity template engine.

MuleSoft's Mule flow will look as shown below. In this flow, we will get input parameters from an HTTP component, then we will call a custom Java class where the Velocity engine will replace static content with input values and return the payload which will be used in the email's body content.

muleflow

Parse Template 

If you don’t know much more about the Velocity template and still want to achieve dynamic content templating, MuleSoft came up with a great component - Parse Template.

Parse Template accepts template file locations which will have content similar to what I've depicted below: 

Hello #[payload.firstname],#[message.inboundProperties.username]
    You have just submitted file: #[payload.fileName].
   Have a great day.
 Regards,
#[flowVars.sender]

Before we call this Parse Template component, set the values in payloadflowVars, andinboundproperties.

I loved this Parse Template feature. You can test both the examples with the code I've provided on my GitHub repo.

I hope this will help you in your project.

Learn about the Five Steps to API Monitoring Success with Runscope

Topics:
mulesoft ,integration ,velocity template ,spring application

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}