Consume Apache CXF, Spring based SOAP Webservice

This tutorial will show you how you can consume SOAP based webservice which is built using Apache cxf, Spring. This is client side application and it consumes the service which I build earlier. The server side application is here at https://roytuts.com/create-soap-webservice-using-apache-cxf-spring/

I am using gradle and maven as build tools for this application.

Prerequisites

Java at least 8, Gradle 6.5.1, Maven 3.6.3, Apache CXF, Java Activation

Spring Apache CXF SOAP Service

Project Setup

You can create either gradle or maven based project in your favorite IDE or tool. The name of the project is spring-soap-webservice-apache-cxf-consumer.

If you are creating gradle based project then use below build.gradle script:

buildscript {
    repositories {
    	mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'no.nils:wsdl2java:0.12'
    }
}

plugins {
    id 'java-library'
    id 'no.nils.wsdl2java' version "0.12"
}

repositories {
    mavenCentral()
}

sourceCompatibility = 12
targetCompatibility = 12

configurations {
    cxf
}

wsdl2java {
	wsdlDir=file("${projectDir}/src/main/resources/wsdl/")
	wsdlsToGenerate = [
		[file("${projectDir}/src/main/resources/wsdl/productService.wsdl")]
	]
	locale = Locale.ENGLISH
}

dependencies {
	cxf 'org.apache.cxf:cxf-rt-frontend-jaxws:3.3.7'
	cxf 'org.apache.cxf:cxf-rt-transports-http:3.3.7'
	
	implementation 'com.sun.activation:javax.activation:1.2.0'
	implementation 'org.apache.cxf:cxf-rt-frontend-jaxws:3.3.7'
	implementation 'org.apache.cxf:cxf-rt-transports-http:3.3.7'
	implementation 'org.jvnet.jaxb2_commons:jaxb2-basics-runtime:1.11.1'
	
	wsdl2java 'org.jvnet.jaxb2_commons:jaxb2-basics-runtime:1.11.1'
    wsdl2java 'org.jvnet.jaxb2_commons:jaxb2-basics:1.11.1'
}

If you are creating maven based project then use below pom.xml file:

<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>com.roytuts</groupId>
    <artifactId>spring-soap-webservice-apache-cxf-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
	
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cxf.version>3.3.7</cxf.version>
    </properties>
	
    <dependencies>
        <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>javax.activation</artifactId>
            <version>1.2.0</version>
        </dependency>
        <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>
    </dependencies>
	
    <build>
        <plugins>
            <!-- maven compiler plugin definition -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
            <!-- Generate Java classes from WSDL during build -->
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>${cxf.version}</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <!-- which source folder the generated classes should be placed in a package -->
                            <sourceRoot>${project.basedir}/src/main/java</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <!-- put the wsdl file in this location -->
                                    <wsdl>${project.basedir}/src/main/resources/wsdl/productService.wsdl</wsdl>
                                    <extraargs>
                                        <extraarg>-client</extraarg>
                                    </extraargs>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Add generated sources - avoids having to copy generated sources to
                build location -->
            <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>${project.basedir}/src/main/java</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- Build the JAR with dependencies -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

WSDL File

Make sure you download the WSDL file (productService.wsdl) and put it under class path folder src/main/resources/wsdl.

Build Project

Now build the project using gradlew clean wsdl2java build command if your project is gradle project or mvn clean install if your project is maven project.

Once build is successful you will see the generated classes in the respective build directory.

Main Class

Finally I am creating the following class which is having main method to consume the service.

package com.roytuts.spring.soap.webservice.apache.cxf.consumer;

import java.util.List;

import com.roytuts.spring.soap.webservice.apache.cxf.service.Product;
import com.roytuts.spring.soap.webservice.apache.cxf.service.ProductService;
import com.roytuts.spring.soap.webservice.apache.cxf.service.ProductService_Service;

public class ProductServiceConsumer {

	public static void main(String[] args) {
		ProductService productService = new ProductService_Service().getProductServiceImplPort();

		List<Product> products = productService.getAllProducts();

		products.forEach(p -> System.out.println(
				"Id: " + p.getProductId() + ", Name: " + p.getProductName() + ", Category: " + p.getProductCatg()));

		Product p = productService.getProduct(101);
		System.out.println("Producte for id 101: " + "Id: " + p.getProductId() + ", Name: " + p.getProductName()
				+ ", Category: " + p.getProductCatg());
	}

}

Testing the Application

Running the above class will give you the following output:

Id: 101, Name: Laptop, Category: Electronics
Id: 102, Name: Bannana, Category: Fruits
Id: 103, Name: Pencil, Category: Stationary
Producte for id 101: Id: 101, Name: Laptop, Category: Electronics

Source Code

Download

Thanks for reading.

Leave a Comment