Spring and CfX by Example
-
Upload
martin-nad -
Category
Documents
-
view
6.697 -
download
0
description
Transcript of Spring and CfX by Example
SPRING AND CXF
CXF by example by Martin Nad [email protected]
Spring and CXF
OVERVIEW
This paper is focused to the CXF and using whitin Spring.
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
The third one is just another example in different aspect.
Page 1
Spring and CXF
TABLE OF CONTEXT:
SPRING-CXF HELLO WORLD
SPRING-CXF SEND PARAMETERS
SPRING-CXF AN OTHER EXAMPLE
Page 2
Spring and CXF
Spring and CXFHere, I will show very shortly and easily how to use CXF in Spring, step by step.
As you know there are several ways to implement webservice with CXF in java
For example Pojo and with help of annotation
First I will show to how make webservice in java annotation and the exactly same application with POJO method later.
Hello world with java annotation
The pom-file should looks like this:
<?xml version="1.0"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.cxf.samples</groupId>
<artifactId>CXF-spring</artifactId>
<version>1.0</version>
<name>Spring CFX Example</name>
<packaging>war</packaging>
<developers>
<developer>
pom-file
<id>Mn</id>
<name>Martin Nad</name>
<email></email>
<organization></organization>
Page 3
Spring and CXF
<organizationUrl></organizationUrl>
<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>
Page 4
Spring and CXF
<id>java.net</id>
<url>http://download.java.net/maven/1/</url>
<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
Page 5
Spring and CXF
You need an interface for you servie and it can look like this one:
package org.martin;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHello(String text);
}
Hello world implementation
And the implementation of helloword-interface can look like this one
package org.martin;
import javax.jws.WebService;
@WebService(endpointInterface = "org.martin.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHello(String text) {
System.out.println("server called, and argument name is: "+text);
return "Hello " + text;
}
}
Application-context
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
Page 6
Spring and CXF
Application-context
<jaxws:endpoint
id="helloWorld"
implementor="org.martin.HelloWorldImpl"
address="/HelloWorld" />
</beans>
Web.xml
And the web.xml can look like:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"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>
Page 7
Spring and CXF
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</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
Check this url and see if you see any wsdl
http://localhost:8080/cfx-spring-1.0/HelloWorld?wsdl
And now you need a client to call the service..
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>
Page 8
Spring and CXF
<name>cfxspringClient</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<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>
Page 9
Spring and CXF
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.6.0</version>
<scope>provided</scope>
</dependency>
<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>
Page 10
Spring and CXF
Client-pom
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<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>
Page 11
Spring and CXF
<source>1.5</source>
<target>1.5</target>
</configuration>
<executions>
<execution>
<id>test</id>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
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>
Page 12
Spring and CXF
<execution>
Client-pom
<id>1</id>
<phase>generate-sources</phase>
<goals>
<goal>wsdl2java</goal>
</goals>
<configuration>
<staleMillis>1</staleMillis>
<urls>
<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>
Page 13
Spring and CXF
</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!
*
*/
public class App
{
public static void main( String[] args ) throws Exception
{
App app = new App();
app.test1HelloWorldImplPortSayHi("martin");
}
public void test1HelloWorldImplPortSayHi(String name) throws Exception {
dk.martin.helloWord.HelloWorldImplServiceSoapBindingStub binding;
try {
binding = (dk.martin.helloWord.HelloWorldImplServiceSoapBindingStub)
new dk.martin.helloWord.HelloWorldImplServiceLocator().getHelloWorldImplPort();
Page 14
Spring and CXF
}
catch (javax.xml.rpc.ServiceException jre) {
if(jre.getLinkedCause()!=null)
jre.getLinkedCause().printStackTrace();
throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);
}
binding.setTimeout(60000);
java.lang.String value = null;
value = binding.sayHi(name);
System.out.println("value is: "+value);
}
}
information
And last if you run your client it should work....
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 donatehttps://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.if it doesn’t work copy the link and put it on your browser.
Page 15
Spring and CXF
MY OTHER DOCUMENTATION
Properties in Java and Spring by Martin Nad
Spring and Cxf by Example
Jboss and Perm Gen Error
Using Ftp Service With Spring
JunIt
How to use Maven
ReFactoring
Maven and Custome Archetype
How to Write Effective Code
How to use caching system in java
Page 16