Spring and CFX by Example
Spring and CFX by Example
CXF
OVERVIEW
The first example is a simple classic hello application, to show what is the minimal
requirement to use CXF with the Spring.
The second example is showing how you can send the parameters to the web service
Page 2
Spring and CXF
TABLE OF CONTEXT:
Page 3
Spring and CXF
As you know there are several ways to implement webservice with CXF in java
First I will show to how make webservice in java annotation and the exactly
same application with POJO method later.
<?xml version="1.0"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.cxf.samples</groupId>
<artifactId>CXF-spring</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<developers>
<developer>
pom-file
<id>Mn</id>
<name>Martin Nad</name>
<email></email>
<organization></organization>
<organizationUrl></organizationUrl>
Page 4
Spring and CXF
<timezone>1</timezone>
<roles>
<role>Architecture disgner</role>
</roles>
</developer>
</developers>
<properties>
<cxf.version>[2,)</cxf.version>
</properties>
pom-file
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
pom-file
<repository>
<id>java.net</id>
<url>http://download.java.net/maven/1/</url>
Page 5
Spring and CXF
<layout>legacy</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
pom-file
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-remoting</artifactId>
<version>2.0.8</version>
</dependency>
</dependencies>
</project>
helloWorld service
You need an interface for you servie and it can look like this one:
package org.martin;
import javax.jws.WebService;
Page 6
Spring and CXF
@WebService
package org.martin;
import javax.jws.WebService;
@WebService(endpointInterface = "org.martin.HelloWorld")
Application-context
<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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
Application-context
<jaxws:endpoint
id="helloWorld"
implementor="org.martin.HelloWorldImpl"
Page 7
Spring and CXF
address="/HelloWorld" />
</beans>
Web.xml
<!DOCTYPE web-app
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContex.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Web.xml
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
Page 8
Spring and CXF
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Build package
Now you should build package and make sure the .war file is on the deploy
folder at jboss server
http://localhost:8080/cfx-spring-1.0/HelloWorld?wsdl
Client-pom
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dk.martin.project</groupId>
<artifactId>cfxspringClient</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>cfxspringClient</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
Page 9
Spring and CXF
<artifactId>junit</artifactId>
<version>4.4</version>
<!--<scope>compile</scope>-->
Client-pom
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>provided</scope>
Client-pom
</dependency>
<dependency>
<groupId>wss4j</groupId>
<artifactId>wss4j</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.6.0</version>
<scope>provided</scope>
</dependency>
Page 10
Spring and CXF
<dependency>
Client-pom
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
Client-pom
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
Page 11
Spring and CXF
<artifactId>servlet-api</artifactId>
<version>2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency> </dependencies>
Client-pom
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<finalName>cfxspringClient</finalName>
<source>1.5</source>
<target>1.5</target>
</configuration>
<executions>
<execution>
<id>test</id>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
Page 12
Spring and CXF
Client-pom
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<executions>
<execution>
Client-pom
<id>1</id>
<phase>generate-sources</phase>
<goals>
<goal>wsdl2java</goal>
</goals>
<configuration>
<staleMillis>1</staleMillis>
<urls>
Page 13
Spring and CXF
<url>http://localhost:8080/cfx-spring-1.0/HelloWorld?wsdl</url>
</urls>
<outputDirectory>${basedir}/src/main/java/</outputDirectory>
<packageSpace>dk.martin.helloWord</packageSpace>
<testCases>false</testCases>
Client-pom
<execution>
Client-pom
<id>2</id>
<phase>generate-sources</phase>
<goals>
<goal>wsdl2java</goal>
</goals>
<configuration>
Client-pom
</plugins>
</build>
</project>
I’m sure you don’t need all element in this pom. But for simplifying I put all
information in this pom to know how you can work with client-side-pom.
When you are finish with your pom. Just run the empty application.
It will automatically call your service and convert your wsdl to java and make for
you a folder hirarchey
Client-java
And now just write client with help of the java-files as follows...
package dk.martin.project.cfxspringClient;
/**
* Hello world!
Page 14
Spring and CXF
*/
app.test1HelloWorldImplPortSayHi("martin");
dk.martin.helloWord.HelloWorldImplServiceSoapBindingStub binding;
try {
binding = (dk.martin.helloWord.HelloWorldImplServiceSoapBindingStub)
new
dk.martin.helloWord.HelloWorldImplServiceLocator().getHelloWorldImp
lPort();
if(jre.getLinkedCause()!=null)
jre.getLinkedCause().printStackTrace();
binding.setTimeout(60000);
value = binding.sayHi(name);
Page 15
Spring and CXF
information
If you want the both client and server example just sends a email to me
[email protected] and I will send you the both example
DONATION
For donation you can just donate 7$ by using this
https://www.paypal.com/cgi-bin/webscr?cmd=_s-
xclick&hosted_button_id=5814019 or use this to put any amont
you like to donate
https://www.paypal.com/cgi-bin/webscr?cmd=_s-
xclick&hosted_button_id=5813954 and if it doesn’t work copy
the link and put it on your browser.
Page 16
Spring and CXF
MY OTHER DOCUMENTATION
JunIt
ReFactoring
Page 17