These are the steps to setup an Eclipse project for a SOAP web service using Apache CXF:
1) Create a New Eclipse Project
Create a new Maven project from eclipse using the maven-archetype-webapp.
Set the targeted runtime by right-clicking on the project and selecting: Properties > Targeted Runtimes. Select a server from the list or configure a new one.
2) Add CXF Dependencies in the pom.xml File
Edit the pom.xml file to add the CXF dependencies, the maven-compiler-plugin, and the maven-war-plugin. The file should contain the following:
<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>com.my.group.id</groupId> <artifactId>MyProject</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ProjectName Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.0.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.1</version> <configuration> <webXml>src/main/webapp/WEB-INF/web.xml</webXml> </configuration> </plugin> </plugins> <finalName>ProjectName</finalName> </build> </project>
3) Add the CXF Servlet to the web.xml File
The project will have a web.xml file under src/main/webapp/WEB-INF/. Add the CXF servlet with the following:
<!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> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
4) Create a class that will handle web service requests
Create a web service class using the @WebService and @WebMethod annotations:
import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class HelloWorldWS { @WebMethod public String sayHello(){ return "Hello... Nice to meet you!"; } }
5) Configure the CXF Servlet
The CXF servlet should be configured through a file cxf-servlet.xml that should be under src/main/webapp/WEB-INF/ . A basic configuration for an annotated class would be as follows:
<?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" xmlns:cxf="http://cxf.apache.org/core" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:server id="MyServiceWS" address="/myservice"> <jaxws:serviceBean> <bean class="my.annotated.class.name.goes.here.HelloWorldWS" /> </jaxws:serviceBean> </jaxws:server> </beans>
6) Deploy and Run
Deploy with Run As > Maven Install. Run with Run As > Run on server.