Saturday, March 15, 2014

Mule REST on Tomcat (via Jersey)

I. Dependencies
  1. Mule server libraries (Mule standalone 3.3.x or later)
  2. Tomcat
  3. Eclipse (any IDE capable of the same)
II. Prerequisites
  1. Install Mule on Tomcat
  2. Create a Dynamic Web Application project in Eclipse and set Tomcat as deploy server
III. Design


IV. Service Class
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)

4 comments:

  1. Hello, Nice article.
    I am however facing an issue when I follow your exact steps.

    I get a XML namespace related error when deploying the mule servlet war file to Tomcat. The error message looks like below:

    Caused by: org.mule.api.lifecycle.InitialisationException: Line 23 in XML document from test-servlet.xml is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 37; cvc-complex-type.2.4.a: Invalid content was found starting with element 'jersey:resources'. One of '{"http://www.mulesoft.org/schema/mule/core":abstract-message-processor, "http://www.mulesoft.org/schema/mule/core":abstract-outbound-endpoint, "http://www.mulesoft.org/schema/mule/core":abstract-mixed-content-message-processor, "http://www.mulesoft.org/schema/mule/core":response}' is expected.

    I think I have added the XML namespaces properly in my test-servlet.xml file:

















    Do you have any idea what could be wrong?

    ReplyDelete
    Replies
    1. late reply, but perhaps, you're missing the jersey namespace?

      xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey"
      http://www.mulesoft.org/schema/mule/jersey
      http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd

      or probably you missed the mule jersey jar.

      Delete
  2. Why @Context HttpServletRequest returns null always?

    ReplyDelete
    Replies
    1. hi prasad. I do hope that you resolved your problem already. In any case, you may have misconfigured something, you can elaborate further so we can understand why yours is failing.

      Delete