Peter Arockiaraj

Can you hear me now?…Good!

Developing Web Services by using Mule, CXF, and Spring


Introduction

In this article we are going to develop a web service by using Spring with CXF and the Mule Enterprise Service Bus. This article provides steps (step by step) to create &  deploy web services by using Spring,CXF and mule. Please go through below to find sample web services.

Software Requirements

  1. EClipse (Java IDE)- Optional
  2. Mule ESB (Download from http://www.mulesoft.org/display/MULE/Download)
  3. CXF Jars (Required for Compilation-Download from http://cxf.apache.org/download.html)

The Code

In this example, we are going to create a Student service, where students can register for particular course. In this example, we are going to use a code-first approach for this service using JAX-WS annotations.

First, we will need to create following classes and interface

  1. Our User-Defined Exception
  2. The interface for our service
  3. The implementation class for our service
  4. Our Student bean

Step 1: Create new java project in EClipse

Step 2: Create the Exception class for our service (NotEnoughValueException.java)

package com.peter.cxf.sample;

public class NotEnoughValueException extends Exception {

String detail;

public NotEnoughValueException (String message, String detail) {

super (message);

this.detail = detail;

}

public String getDetail () {

return detail;

}

}

Step 3: Create the interface for our service (StudentService.java)

package com.peter.cxf.sample;

import javax.jws.WebParam;

import javax.jws.WebResult;

import javax.jws.WebService;

@WebService

public interface StudentService {

@WebResult(name = “registrationstatus”)

public String registerStudent(@WebParam(name = “student”) Student stu)

throws NotEnoughValueException;

}

Step 4: Create the implementation class for our service (StudentServiceImpl.java)

package com.peter.cxf.sample;

import javax.jws.WebService;

@WebService(endpointInterface = “com.peter.cxf.sample.StudentService”, serviceName = “StudentService”)

public class StudentServiceImpl {

/**

*

* @param stu

* @return

* @throws NotEnoughValueException

*/

public String registerStudent(Student stu) throws NotEnoughValueException {

if (validate(stu)) {

return “Hi ” + stu.getFirstName() + ” ” + stu.getLastName()

+ ” Your registertion is accepted. You Can join the course”;

} else {

throw new NotEnoughValueException(“Input values are not enough”,

“Please check you data”);

}

}

private boolean validate(Student stu) {

if ((stu.getFirstName() != null || stu.getFirstName().trim().equals(“”))

&& (stu.getLastName() != null || stu.getLastName().trim()

.equals(“”))

&& (stu.getPhoneNumber() != null || stu.getPhoneNumber().trim()

.equals(“”)))

return true;

else

return false;

}

}

Configuration

To run our service under Mule, we need to create following configuration files:

  1. Spring configuration file
  2. Mule configuration file

Step 5: Create the Spring Configuration file in project root folder (studentContext.xml)

<?xml version=“1.0” encoding=“UTF-8”?>

<beans xmlns=http://www.springframework.org/schema/beans&#8221;

xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;

xsi:schemaLocation=http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&#8221;>

<bean id=“studentService” class=“com.peter.cxf.sample.StudentServiceImpl”

scope=“singleton”>

</bean>

</beans>

Step 6: Create the Mule Configuration file in project root folder (studentservice-config.xml)

<?xml version=“1.0” encoding=“UTF-8”?>

<mule xmlns=http://www.mulesource.org/schema/mule/core/2.2&#8221;

xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:spring=http://www.springframework.org/schema/beans&#8221;

xmlns:soap=http://www.mulesource.org/schema/mule/soap/2.2&#8221; xmlns:cxf=http://www.mulesource.org/schema/mule/cxf/2.2&#8221;

xsi:schemaLocation=

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/soap/2.2 http://www.mulesource.org/schema/mule/soap/2.2/mule-soap.xsd

http://www.mulesource.org/schema/mule/cxf/2.2 http://www.mulesource.org/schema/mule/cxf/2.2/mule-cxf.xsd&#8221;>

<spring:beans>

<spring:import resource=“studentContext.xml” />

</spring:beans>

<model name=“services”>

<service name=“StudentService”>

<inbound>

<cxf:inbound-endpoint

address=http://localhost:65082/services/StudentService&#8221; />

</inbound>

<component>

<spring-object bean=“studentService” />

</component>

</service>

</model>

</mule>

Build and Deployment

Step 7: Create the folder ‘ant’ inside the project root folder

Step 8: Create the build.xml file inside ant folder.

<?xml version=”1.0″ encoding=”UTF-8″?>

<project name=”ws” basedir=”../” default=”archive”>

<target name=”archive”>

<jar destfile=”cxfstudentservices.jar”>

<fileset dir=”${basedir}\bin”>

<include name=”**/*.class” />

</fileset>

<fileset dir=”${basedir}”>

<include name=”**/*.xml” />

<exclude name=”**/*build*” />

</fileset>

</jar>

</target>

</project>

Step 9: run Ant build

Step 10: Copy cxfstudentservices.jar file from project root folder and copy it in MULE_HOME/lib/user folder.

Starting the service

To start the service in mule you have run following command in command prompt.

In Windows: MULE_HOME/bin/mule.bat -config <path-to-config-file>\studentservice-config.xml

For Example:

c:\mule-standalone-2.2.1\bin\mule.bat -config D:\Java Code\CXFMuleStudentService\studentservice-config.xml

Please ensure that your service is up and running by opening following url in your web browser.

http://localhost:65082/services/StudentService?wsdl

Testing the service

To test your service, please check my previous posts, to create client program for web services.

November 5, 2009 - Posted by | Web Services

2 Comments »

  1. Very great site.
    The content here is genuinely valuable.

    I will tell my friends.

    Cheers

    Comment by whey.protein.side.effects | November 30, 2009 | Reply

  2. paglachoda pod mar , chondon tutorial hoeche chole na.

    Comment by gandu | November 7, 2011 | Reply


Leave a comment