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

Creating REST Service Using Mule ESB 3.3

DZone's Guide to

Creating REST Service Using Mule ESB 3.3

· Integration Zone
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.

Creating Rest Services with Mule is very easy as mule provides built-in support for Jersey.

Create mule flow in mule studio like this.


First, create a REST class like this and link it to the REST component:
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;


@Path("restClass")
public class RestClass {

 public Response getExample(@QueryParam("param1")String param1)
 {
  return Response.status(Status.OK).entity("hello " + param1).build();
 }
 
}
This is how the Mule flow xml will look:
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey" 
xmlns:http="http://www.mulesoft.org/schema/mule/http" 
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="CE-3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.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 ">
    <flow name="restTestFlow1" doc:name="restTestFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" 
host="localhost" port="8081" doc:name="HTTP"/>
        <jersey:resources doc:name="REST">
            <component class="RestClass"/>
         </jersey:resources>
    </flow>
</mule>
If you want to use the created spring bean in the rest Component, then first declare the component as a spring bean and then refer it in the jersey-resource using spring-object tag , like this:
<flow name="restTestFlow1" doc:name="restTestFlow1">

     <http:inbound-endpoint exchange-pattern="request-response" host="localhost"
 port="8081" doc:name="HTTP"/>
       <spring:bean id="testBean" class="TestSpringBean"></spring:bean>
    <spring:bean id="restClass">
      <spring:property name="bean" ref="testBean"></spring:property>
    </spring:bean>

        <jersey:resources doc:name="REST">
            <component doc:name="rest component">
             <spring-object bean="restClass">
            </component>
        </jersey:resources>
</flow>
You can use multiple REST classes also , like this:
<jersey:resources doc:name="REST">
   <component>
    <spring-object bean="restService" />
   </component>
   <component>
    <spring-object bean="restService1" />
   </component>
  </jersey:resources>
Mule Studio throws error "Required attribute class is not defined in component" , you can ignore this error, as it runs perfectly fine.

Post your suggestions !!


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:

Published at DZone with permission of Abhishek Somani. See the original article here.

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 }}