This blog illustrates how to configure and develop a web service and client using Apache CXF.
Step 1
Download and Install JDK 1.6, Maven and your favorite web application container(tomcat,jboss,glassfish).
Step 2
Create a maven application with archetype "maven-archetype-webapp". Ask Google if you need help running this archetype.
Step 3
Add maven repository for JBoss and dependencies for junit and Apache CXF.
pom.xml
Step 4
Develop the webservice interface and class using JAX-WS annotations.
CalculatorService.java
CalculatorServiceImpl.java
Step 5
Configure spring context loader and Apache's CXF Servlet in web.xml.
web.xml
Step 6
Configure calculator service endpoint in spring's context by specifying service class and URL.
applicationContext.xml
Step 7
Run the application in your webapp container and test it using the following URL
http://localhost:<Port>/<WebAppContext>/services/calculator?WSDL
Step 8
Configure Apache CXF's JaxWsProxyFactoryBean class to return a service interface to our webservice and call methods on the web service directly.
CalculatorWebServiceClientTest.java
Download Project
You can download my project from google code. SVN URL is http://ingenious-camel.googlecode.com/svn/trunk/SpringCxfWSApp/
Step 1
Download and Install JDK 1.6, Maven and your favorite web application container(tomcat,jboss,glassfish).
Step 2
Create a maven application with archetype "maven-archetype-webapp". Ask Google if you need help running this archetype.
Step 3
Add maven repository for JBoss and dependencies for junit and Apache CXF.
pom.xml
.
.
<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Maven Repository Group</name>
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<properties>
<cxf.version>2.4.0</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
.
.
Step 4
Develop the webservice interface and class using JAX-WS annotations.
CalculatorService.java
@WebService
public interface CalculatorService {
double addNumbers(double a, double b);
double subtractNumbers(double a, double b);
}
CalculatorServiceImpl.java
@WebService(endpointInterface = "com.ingeniouscamel.springcxfws.service.CalculatorService",
serviceName = "calculatorService")
public class CalculatorServiceImpl implements CalculatorService {
public double addNumbers(double a, double b) {
return a + b;
}
public double subtractNumbers(double a, double b) {
return a - b;
}
}
Step 5
Configure spring context loader and Apache's CXF Servlet in web.xml.
web.xml
. . <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>cxfws-servlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>cxfws-servlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> . .
Step 6
Configure calculator service endpoint in spring's context by specifying service class and URL.
applicationContext.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:endpoint id="calculatorService" implementor="com.ingeniouscamel.springcxfws.service.CalculatorServiceImpl" address="/calculator"/> </beans>
Step 7
Run the application in your webapp container and test it using the following URL
http://localhost:<Port>/<WebAppContext>/services/calculator?WSDL
Step 8
Configure Apache CXF's JaxWsProxyFactoryBean class to return a service interface to our webservice and call methods on the web service directly.
CalculatorWebServiceClientTest.java
public class CalculatorWebServiceClientTest {
private CalculatorService calculatorWebService;
@Before
public void setUp() throws Exception {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(CalculatorService.class);
factory.setAddress("http://localhost:8080/SpringCxfWSApp/services/calculator");
calculatorWebService = (CalculatorService) factory.create();
}
@Test
public void testAddNumbers() {
Assert.isTrue(calculatorWebService.addNumbers(3, 5) == 8);
}
@Test
public void testSubtractNumbers() {
Assert.isTrue(calculatorWebService.subtractNumbers(7, 4) == 3);
}
}
Download Project
You can download my project from google code. SVN URL is http://ingenious-camel.googlecode.com/svn/trunk/SpringCxfWSApp/

No comments:
Post a Comment