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
- EClipse (Java IDE)- Optional
- Mule ESB (Download from http://www.mulesoft.org/display/MULE/Download)
- 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
- Our User-Defined Exception
- The interface for our service
- The implementation class for our service
- 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:
- Spring configuration file
- 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”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd”>
<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”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:spring=“http://www.springframework.org/schema/beans”
xmlns:soap=“http://www.mulesource.org/schema/mule/soap/2.2” xmlns:cxf=“http://www.mulesource.org/schema/mule/cxf/2.2”
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”>
<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” />
</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.
2 Comments »
Leave a comment
-
Recent
- Integrating JSF, Spring Security and LDAP
- Developing Web Services by using Mule, CXF, and Spring
- Developing Web Services by Using Metro Webservices Framework
- Developing CXF WS-Security with Spring & Acegi Security
- Developing CXF WS-Security with SAML
- Developing CXF WS-Security with Signature(Certificates)
- Developing CXF Web services with WS-Security
- RESTful web services using the Jersey framework
- Developing Web Services By Using Spring and CXF
- Developing Web Services by Using Spring and XFire
- Developing Web services Using Spring Framework
- Developing Simple Web Services by Using JWSDP
-
Links
-
Archives
- January 2010 (1)
- November 2009 (1)
- October 2009 (1)
- September 2009 (9)
-
Categories
-
RSS
Entries RSS
Comments RSS
Very great site.
The content here is genuinely valuable.
I will tell my friends.
Cheers
Comment by whey.protein.side.effects | November 30, 2009 |
paglachoda pod mar , chondon tutorial hoeche chole na.
Comment by gandu | November 7, 2011 |