You are on page 1of 2

Creating REST Service Using Mule ESB 3.

3
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: view source print?

01.import javax.ws.rs.Path; 02.import javax.ws.rs.QueryParam; 03.import javax.ws.rs.core.Response; 04.import javax.ws.rs.core.Response.Status; 05. 06. 07.@Path("restClass") 08.public class RestClass { 09. 10.public Response getExample(@QueryParam("param1")String param1) 11.{ 12.return Response.status(Status.OK).entity("hello " + param1).build(); 13.} 14. 15.}

This is how the Mule flow xml will look: view source print?
01.<?xml version="1.0" encoding="UTF-8"?> 02. 03.<mule xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey" 04.xmlns:http="http://www.mulesoft.org/schema/mule/http" 05.xmlns="http://www.mulesoft.org/schema/mule/core" 06.xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" 07.xmlns:spring="http://www.springframework.org/schema/beans" version="CE3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="

08.http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 09.http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd 10.http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 11.http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd "> 12.<flow name="restTestFlow1" doc:name="restTestFlow1"> 13.<http:inbound-endpoint exchange-pattern="request-response" 14.host="localhost" port="8081" doc:name="HTTP"/> 15.<jersey:resources doc:name="REST"> 16.<component class="RestClass"/> 17.</jersey:resources> 18.</flow> 19.</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: view source print?
01.<flow name="restTestFlow1" doc:name="restTestFlow1"> 02. 03.<http:inbound-endpoint exchange-pattern="request-response" host="localhost" 04.port="8081" doc:name="HTTP"/> 05.<spring:bean id="testBean" class="TestSpringBean"></spring:bean> 06.<spring:bean id="restClass"> 07.<spring:property name="bean" ref="testBean"></spring:property> 08.</spring:bean> 09. 10.<jersey:resources doc:name="REST"> 11.<component doc:name="rest component"> 12.<spring-object bean="restClass"> 13.</component> 14.</jersey:resources> 15.</flow>

You can use multiple REST classes also , like this: view source print?
1.<jersey:resources doc:name="REST"> 2.<component> 3.<spring-object bean="restService" /> 4.</component> 5.<component> 6.<spring-object bean="restService1" /> 7.</component> 8.</jersey:resources>

Mule Studio throws error "Required attribute class is not defined in component" , you can ignore this error, as it runs perfectly fine.

You might also like