Apache CXF Soap Web Service Example



This example explains how to implement a Soap Web Service using Apache CXF.
Following are the configuration and implementation details used in this example.

Step 1: Create Maven project

Following pom.xml defines the dependencies for this example.
		
<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.lal.pro</groupId> <artifactId>apache-cxf-soap-ws</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>apache-cxf-soap-ws Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <cxf.version>2.5.0</cxf.version> <org.springframework.version>4.1.1.RELEASE</org.springframework.version> <ch.qos.logback.version>1.1.3</ch.qos.logback.version> <org.sl4j.version>1.7.12</org.sl4j.version> <spring.ws.version>2.2.4.RELEASE</spring.ws.version> </properties> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.1</version> </dependency> </dependencies> <build> <finalName>apache-cxf-soap-ws</finalName> </build> </project>


Make sure your soap web service is running and

Step 2: Create web.xml

Following web.xml must be placed at "/apache-cxf-rest-ws/src/main/webapp/WEB-INF/".
		
<!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>CXF</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ApplicationContext-cxf.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>


Step 3: Create ApplicationContext

In the following xml we can define the beans, also group the web services and specify In and out interceptors. Place this xml file at /apache-cxf-soap-ws/src/main/resources/. Make sure it is added in your web.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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cxf="http://cxf.apache.org/core" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-xml.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <task:annotation-driven /> <context:component-scan base-package="com.smoothexample"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Component" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> </context:component-scan> <context:annotation-config /> <context:component-scan base-package="com.smoothexample" /> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <bean id="addressSoapService" class="com.smoothexample.cxf.soap.ws.impl.AddressSoapServiceImpl" /> <jaxws:endpoint id="addressEndpointId" implementorClass="com.smoothexample.cxf.soap.ws.impl.AddressSoapServiceImpl" implementor="#addressSoapService" address="/addressSoapService"> </jaxws:endpoint> </beans>


Step 4: Create a data transfer object

Following DTO can be used in request and response.
		
package com.lal.pro.dto; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Address { private String streetAddress; private String addressOptional; private String city; private String state; private String country; private String zip; public String getStreetAddress() { return streetAddress; } public void setStreetAddress(String streetAddress) { this.streetAddress = streetAddress; } public String getAddressOptional() { return addressOptional; } public void setAddressOptional(String addressOptional) { this.addressOptional = addressOptional; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } }


Step 5: Define Web Service

Following Interface defines the web service
		

package com.smoothexample.cxf.soap.ws; import javax.jws.WebService; import com.smoothexample.dto.Address; @WebService(endpointInterface = " com.smoothexample.cxf.soap.ws.AddressSoapService", serviceName = "addressSoapService") public interface AddressSoapService { public Address getAddress() throws Exception; }


Implement Web Service

Following code snippet shows the implementaion of Web Service . It just creates a java object and returns it.
			
package com.smoothexample.cxf.soap.ws.impl; import com.smoothexample.cxf.soap.ws.AddressSoapService; import com.smoothexample.dto.Address; public class AddressSoapServiceImpl implements AddressSoapService { public Address getAddress() { return createAddress(); } private Address createAddress() { Address address = new Address(); address.setStreetAddress("4800 abc Rd"); address.setCity("abcd"); address.setState("NJ"); address.setCountry("US"); address.setZip("10001"); address.setAddressOptional("addressOptional"); return address; } }


Generated WSDL

			

<wsdl:definitions xmlns:ns1="http://ws.soap.cxf.smoothexample.com/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.ws.soap.cxf.smoothexample.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="AddressSoapServiceImplService" targetNamespace="http://impl.ws.soap.cxf.smoothexample.com/"> <wsdl:import location="http://localhost:8080/apache-cxf-soap-ws/addressSoapService?wsdl=AddressSoapService.wsdl" namespace="http://ws.soap.cxf.smoothexample.com/"></wsdl:import> <wsdl:binding name="AddressSoapServiceImplServiceSoapBinding" type="ns1:AddressSoapService"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="getAddress"> <soap:operation soapAction="" style="document"/> <wsdl:input name="getAddress"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="getAddressResponse"> <soap:body use="literal"/> </wsdl:output> <wsdl:fault name="Exception"> <soap:fault name="Exception" use="literal"/> </wsdl:fault> </wsdl:operation> </wsdl:binding> <wsdl:service name="AddressSoapServiceImplService"> <wsdl:port binding="tns:AddressSoapServiceImplServiceSoapBinding" name="AddressSoapServiceImplPort"> <soap:address location="http://localhost:8080/apache-cxf-soap-ws/addressSoapService"/> </wsdl:port> </wsdl:service> </wsdl:definitions>


Sample request can be used from postman or soap ui

			

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.soap.cxf.smoothexample.com/"> <soapenv:Header/> <soapenv:Body> <ws:getAddress/> </soapenv:Body> </soapenv:Envelope>


Sample Response

			

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:getAddressResponse xmlns:ns2="http://ws.soap.cxf.smoothexample.com/"> <return> <addressOptional>addressOptional</addressOptional> <city>abcd</city> <country>US</country> <state>NJ</state> <streetAddress>4800 abc Rd</streetAddress> <zip>10001</zip> </return> </ns2:getAddressResponse> </soap:Body> </soap:Envelope>