Apache CXF - Simple WebService With Spring
A reader posted a comment to one of my old blogs on XFire. That rekindled my interest so I checked the XFire web site only to be informed that XFire is now Apache CXF (version 2.2.3 at this moment in time).
So how hard would it be to convert my old example to CXF. Turned out to be a piece of cake. Had to change the following:
The web.xml...
The Spring context file - app-context.xml
The WSDL is located at the address mentioned above in the wsld2java command.
Finally I ran the test client that wsdl2java generated...class named EchoService_EchoServicePort_Client
The output was:
So how hard would it be to convert my old example to CXF. Turned out to be a piece of cake. Had to change the following:
- Updated the maven dependencies to reflect CXF libraries.
- web.xml - to point to the CXF Servlet
- Spring context (app-context.xml) - It was now a lot simpler and cleaner.
- Finally I used CXF wsdl2java utils to generate a client
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.aver</groupId>
<artifactId>echo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>CXF Echo Service</name>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<contextPath>/echoservice</contextPath>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9090</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The web.xml...
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/app-context.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
The Spring context file - app-context.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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans-2.0.xsd
cxf.apache.org/jaxws
">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="echoService" implementor="#echo"
address="/EchoService" />
<bean id="echo" class="com.aver.EchoServiceImpl" />
</beansThe wsdl2java command I executed was:./wsdl2java -p com.aver.client -client localhost:9090/echoservice/services/EchoService?wsdl
Run the project using maven: mvn clean package jetty:runThe WSDL is located at the address mentioned above in the wsld2java command.
Finally I ran the test client that wsdl2java generated...class named EchoService_EchoServicePort_Client
The output was:
Invoking echo...
echo.result=echo: 'uyy ' received on 10-07-2009 10:53:59 PM
Click here to download the maven project for this blog.






Comments