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

Expressing the Spring Expression Language via Mule

DZone's Guide to

Expressing the Spring Expression Language via Mule

Mule easily uses the Spring Expression Language (SpEL) in its application. We will be creating a small Mule application to demonstrate the usage of SpEL in a Mule app.

Free Resource

Today’s data climate is fast-paced and it’s not slowing down. Here’s why your current integration solution is not enough. Brought to you in partnership with Liaison Technologies.

We all might be familiar with the Spring Expression Language (SpEL), which is one of the powerful expression languages that supports querying and manipulating an object graph at runtime.

There are also several other expression languages available like OGNL, MVEL, MEL, and JBoss EL, but SpEL is undoubtedly one of the better performer and was created to provide the Spring community with a single well supported expression language that can be used across all the Spring products.

Mule has an old relationship with Spring and supports the Spring framework for developing Java applications. Mule has the advantage of creating and managing Spring application contexts for our Mule application. Mule XML configuration file uses Spring 4.1.6’s ability to create bean definition profiles.

Though Mule has its own expression language called MEL, which is based on the top of MVEL, we will see in this article how Mule easily uses the Spring Expression Language (SpEL) in its application. We will be creating a small Mule application to demonstrate the usage of SpEL in a Mule app.

Let’s start creating our application with following Java class:

package com.service;

public class PathAddress {

       private String path;

       private String basePath;

       public String getPath() {

              return path;

       }

       public void setPath(String path) {

              this.path = path;

       }


       public String getBasePath() {

              return basePath;

       }

       public void setBasePath(String basePath) {

              this.basePath = basePath;

       }



       public String myTestMethod() {

              return "Hi, This is a test method !";

       }

}

We can see that we have defined two String values, path and basePath, that we will set in the URL of our application.

Let's look into our Mule flow:

<spring:beans>

     <spring:bean id="pathversion" class="com.service.PathAddress">

         <spring:property name="path" value="#{'test'}"/>

         <spring:property name="basePath" value="#{ systemProperties['base.path'] }"/>

     </spring:bean>

   </spring:beans>

    <http:listener-config name="HTTP_Listener_Configuration" basePath="#{pathversion.getBasePath()}" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>


     <flow name="Flow">

         <http:listener config-ref="HTTP_Listener_Configuration" path="#{pathversion.getPath()}" doc:name="HTTP"/>

        <set-payload doc:name="Set Payload" value="#{pathversion.myTestMethod()}"/>

         <parse-template location="response.html" doc:name="Parse Template"/>

      </flow>

We can see here that we have defined the Spring bean and passing the values of the variables path and basePath from the bean. We have used SpEL to set the value of the variable elements.

For the path element, we have used SpEL to set the value directly as a string, whereas for the basePath element, we have used SpEL to set the value from the environment variable, which is defined in the app.properties file of our application:

base.path=application

Now, if you look in the XML, you can see that we have used SpEL to assign the values in the basePath for our HTTP listener connector as well as in our path of HTTP listener endpoint in the flow.

Further, we are calling our method myTestMethod() of the Java class using the SpEL.

At the end, we have added a basic HTML file in src/main/resources folder and call it in our flow using parse-template to spice up our response:

<html>

<head>

<title>Spring Expression Language Sample</title>

</head>

<body>

<center>

<b><font size="30" color="red">#[payload]</font></b>

</center>

</body>

</html>

Testing our Application

I we now deploy our application in server and test our application with this URL, we can see the result in the browser:

Image title

Conclusion

As we can see, we can easily use the powerful Spring Expression Language in our Mule application easily. Although Mule has its own MEL expression, we can implement SpEL side-by-side in our application as Mule is built on Spring and it offers flexibility to integrate this SpEL easily in our code.

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

Topics:
mule ,spring ,integration ,expression language

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}