- Mule server libraries (Mule standalone 3.3.x or later)
- Tomcat
- Eclipse (any IDE capable of the same)
II. Prerequisites
- Install Mule on Tomcat
- Create a Dynamic Web Application project in Eclipse and set Tomcat as deploy server
III. Design
IV. Service Class
V. Entity/Model Class
VI. Mule Config
VII. Spring Context
VIII. WEB.XML
package com.seralde.rest.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.seralde.rest.entity.SampleEO;
@Path("/")
public class HelloWorldService {
@GET
public Response defaulMethod(){
String output = "Hello World!";
return Response.status(200).entity(output).build();
}
@GET
@Path("/echo/{param}")
public Response getMsg(@PathParam("param") String msg) {
String output = "Hello " + msg + "!";
return Response.status(200).entity(output).build();
}
@GET
@Path("/eo")
@Produces(MediaType.APPLICATION_JSON)
public SampleEO getEO() {
SampleEO eo = new SampleEO();
eo.setAge(10);
eo.setName("Juan dela Cruz");
return eo;
}
}
V. Entity/Model Class
package com.seralde.rest.entity;
public class SampleEO {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
VI. Mule Config
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:core="http://www.mulesoft.org/schema/mule/core" 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"
xmlns:servlet="http://www.mulesoft.org/schema/mule/servlet"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.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/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/servlet http://www.mulesoft.org/schema/mule/servlet/current/mule-servlet.xsd">
<spring:beans>
<spring:import resource="classpath:sampleRestAppContext.xml"/>
</spring:beans>
<flow name="restappFlow1" doc:name="restappFlow1">
<servlet:inbound-endpoint path="restService" responseTimeout="10000" doc:name="Servlet"/>
<jersey:resources doc:name="REST">
<!--<component class="com.seralde.rest.HelloWorldService"/> -->
<component>
<spring-object bean="helloWorldService"/>
</component>
</jersey:resources>
</flow>
</mule>
VII. Spring Context
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<bean id="helloWorldService" class="com.seralde.rest.service.HelloWorldService"/>
</beans>
VIII. WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>sample-rest</display-name>
<context-param>
<param-name>org.mule.config</param-name>
<param-value>mule-config-rest.xml</param-value>
</context-param>
<listener>
<listener-class>
org.mule.config.builders.MuleXmlBuilderContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>muleServlet</servlet-name>
<servlet-class>
org.mule.transport.servlet.MuleReceiverServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>muleServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
IX. DOWNLOAD SAMPLE PROJECT
Download Project HERE
See MuleSoft (Mule ESB)