Folgende Schritte sind zu tun um ein Webservice basierend auf CXF und Spring zu realisieren:
1. Maven WAR Projekt anlegen:
mvn archetype:create -DpackageName=org.developers.blog.cxfspringexample -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=org.developers.blog -DartifactId=cxf-spring-example
2. pom.xml um folgende Dependencies erweitern:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf</artifactId>
<version>2.1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt</artifactId>
<version>2.1.2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
<build>
<finalName>cxf-spring-example</finalName>
<plugins>
<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>
3. Interface definieren (im Verzeichnis src/main/java/org/developers/blog)
package org.developers.blog;
import javax.jws.WebService;
@WebService(targetNamespace = "http://my.org/ns/")
public interface IServiceExample {
String sayHello(String text);
}
4. Implementierung definieren (im Verzeichnis src/main/java/org/developers/blog)
package org.developers.blog;
import javax.jws.WebService;
@WebService(endpointInterface="org.developers.blog.IServiceExample",targetNamespace = "http://my.org/ns/")
public class ServiceExample implements IServiceExample {
public String sayHello(String text) {
return "Hello: " + text;
}
}
5. Spring Context Datei (spring-context.xml) im WEB-INF Verzeichnis anlegen:
<?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">
<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" />
<bean id="helloExample" class="org.developers.blog.ServiceExample" />
<jaxws:endpoint id="helloEndpoint" implementor="#helloExample" address="/Hello" />
</beans>
6. web.xml um folgende Einträge erweitern:
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
7. Compilieren
mvn clean package ausführen und target/cxf-spring-example.war ins Tomcat-Webapps-Verzeichnis kopieren.
8. Eine Serviceauflistung gibt es direkt unter der Webapp-Root-URI, z.B. http://localhost:8080/cxf-spring-example/
Technorati Tags: Webservice SOA CXF

Klasse Anleitung,
leider hat sich bei 1) ein kleiner TippFehler eingeschlichen ...
<code>mvn archetype:create -DpackageName=org.developers.blog.cxfspringexample -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=org.developers.blog -DartifactId=cxf-spring-example
</code>