APACHE CXF SOAP WEB SERVICE EXAMPLE



This example explains how to implement a Soap Web Service Client using Apache CXF. Before running this application, please make sure a Soap Web Service is running in your machine and wsdl is available at

http://localhost:8080/apache-cxf-soap-ws/addressSoapService?wsdl

Please refer Soap Web Services - Apache CXF for a SOAP Web Service example

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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>apache_cxf_soap_client</groupId> <artifactId>com.smoothexample.pro</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>apache_cxf_soap_client</name> <url>http://maven.apache.org</url> <properties> <org.apache.cxf.version>3.1.3</org.apache.cxf.version> <wsg.url>http://localhost:8080/apache-cxf-soap-ws/addressSoapService?wsdl</wsg.url> </properties> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${org.apache.cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${org.apache.cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-hc</artifactId> <!-- 2.7.8 or 3.0.0-milestone1 --> <version>3.0.0-milestone1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-client</artifactId> <version>3.0.0-milestone1</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>${org.apache.cxf.version}</version> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${basedir}/target/generated/src/main/java</source> </sources> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <configuration> <sourceRoot>${basedir}/target/generated/src/main/java</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${wsg.url}</wsdl> <extraargs> <extraarg>-client</extraarg> <extraarg>-exsh</extraarg> <extraarg>true</extraarg> </extraargs> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>


Step 2:Create Web Service Client

Following Client application is used with the help of generated src using the above pom . Once everything is set up in eclipse you can see the generated src in target folder. Or you can run a maven build
		
package com.smoothexample.pro; /** * Please modify this class to meet your needs * This class is not complete */ import java.io.File; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import com.smoothexample.cxf.soap.ws.AddressSoapService; import com.smoothexample.cxf.soap.ws.impl.AddressSoapServiceImplService; public final class AddressSoapServiceClient { private static final QName SERVICE_NAME = new QName("http://impl.ws.soap.cxf.smoothexample.com/", "AddressSoapServiceImplService"); private AddressSoapServiceClient() { } public static void main(String args[]) throws java.lang.Exception { URL wsdlURL = AddressSoapServiceImplService.WSDL_LOCATION; if (args.length > 0 && args[0] != null && !"".equals(args[0])) { File wsdlFile = new File(args[0]); try { if (wsdlFile.exists()) { wsdlURL = wsdlFile.toURI().toURL(); } else { wsdlURL = new URL(args[0]); } } catch (MalformedURLException e) { e.printStackTrace(); } } AddressSoapServiceImplService ss = new AddressSoapServiceImplService(wsdlURL, SERVICE_NAME); AddressSoapService port = ss.getAddressSoapServiceImplPort(); { System.out.println("Invoking getAddress..."); try { com.smoothexample.cxf.soap.ws.Address address = port.getAddress(); System.out.println("getAddress.result=" + address.getCountry()); } catch (Exception e) { System.out.println("Expected exception: Exception has occurred."); System.out.println(e.toString()); } } System.exit(0); } }


Generated WSDL

This wsdl is just for your reference, location of theWSDL is defined in

	
<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 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>