Spring SOAP WebService Consumers using Gradle

Introduction

This tutorial will show you an example on Spring SOAP WebService Consumers using Gradle. In other words, how we can consume SOAP based web service using Spring and Gradle. We will use Apache CXF to consume the SOAP web service.

This example shows only Spring SOAP web service consumers using Gradle to consume the already deployed or published service.

Prerequisites

Eclipse Neon, Java 1.8, Apache CXF 3.1.10

Spring SOAP WebService Producers using Gradle

Creating Project

Create gradle project called spring-boot-soap-consumer using the following gradle dependencies.

buildscript {
    repositories {
        mavenCentral()
		jcenter()
    }
    dependencies {
        classpath 'no.nils:wsdl2java:0.10'
    }
}
apply plugin: 'java'
apply plugin: 'no.nils.wsdl2java'
repositories {
    mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
wsdl2javaExt {
	cxfVersion = "3.1.10"
}
wsdl2java{
	generatedWsdlDir = file("${projectDir}/src/main/service")
	wsdlDir=file("${projectDir}/src/main/resources/wsdl/")
	wsdlsToGenerate = [
		[file("${projectDir}/src/main/resources/wsdl/users.wsdl")]
	]
}
compileJava.dependsOn wsdl2java

Getting WSDL

Get the WSDL file from http://localhost:9999/ws/users.wsdl and you can save the downloaded file under src/main/resources/wsdl directory.

Please check the Prerequisites section to read the SOAP producer.

The WSDL file will look similar to the below content:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="https://roytuts.com/UserService" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="https://roytuts.com/UserService" targetNamespace="https://roytuts.com/UserService">
  <wsdl:types>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="https://roytuts.com/UserService">
	<xs:element name="getUserDetailsRequest">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="name" type="xs:string"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="getUserDetailsResponse">
		<xs:complexType>
			<xs:sequence>
				<xs:element maxOccurs="unbounded" minOccurs="0" name="users" type="tns:user"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:complexType name="user">
		<xs:sequence>
			<xs:element name="id" type="xs:int"/>
			<xs:element name="name" type="xs:string"/>
			<xs:element name="email" type="xs:string"/>
			<xs:element name="address" type="tns:address"/>
		</xs:sequence>
	</xs:complexType>
	<xs:complexType name="address">
		<xs:sequence>
			<xs:element name="street" type="xs:string"/>
			<xs:element name="city" type="xs:string"/>
			<xs:element name="state" type="xs:string"/>
			<xs:element name="zip" type="xs:int"/>
			<xs:element name="country" type="xs:string"/>
			<xs:element name="addressType" type="tns:addressType"/>
		</xs:sequence>
	</xs:complexType>
	<xs:simpleType name="addressType">
		<xs:restriction base="xs:string">
			<xs:enumeration value="PERMANENT"/>
			<xs:enumeration value="COMMUNICATION"/>
			<xs:enumeration value="OFFICIAL"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>
  </wsdl:types>
  <wsdl:message name="getUserDetailsResponse">
    <wsdl:part element="tns:getUserDetailsResponse" name="getUserDetailsResponse">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getUserDetailsRequest">
    <wsdl:part element="tns:getUserDetailsRequest" name="getUserDetailsRequest">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="UserPort">
    <wsdl:operation name="getUserDetails">
      <wsdl:input message="tns:getUserDetailsRequest" name="getUserDetailsRequest">
    </wsdl:input>
      <wsdl:output message="tns:getUserDetailsResponse" name="getUserDetailsResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="UserPortSoap11" type="tns:UserPort">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getUserDetails">
      <soap:operation soapAction=""/>
      <wsdl:input name="getUserDetailsRequest">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="getUserDetailsResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="UserPortService">
    <wsdl:port binding="tns:UserPortSoap11" name="UserPortSoap11">
      <soap:address location="http://localhost:9999/ws"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

Building the Project

Execute command – gradle clean build on the project root directory from cmd prompt.

You will see the required jar files get downloaded and finally you would get “BUILD SUCCESSFUL” message.

Creating Main Class

We will create below main class to consume the SOAP service.

package com.roytuts.spring.boot.soap.consumer;
import https.www_roytuts_com.userservice.GetUserDetailsRequest;
import https.www_roytuts_com.userservice.GetUserDetailsResponse;
import https.www_roytuts_com.userservice.User;
import https.www_roytuts_com.userservice.UserPort;
import https.www_roytuts_com.userservice.UserPortService;
public class SpringSoapConsumer {
	public static void main(String[] args) {
		UserPort userPort = new UserPortService().getUserPortSoap11();
		GetUserDetailsRequest request = new GetUserDetailsRequest();
		request.setName("souvik");
		GetUserDetailsResponse response = userPort.getUserDetails(request);
		for (User user : response.getUsers()) {
			System.out.println(user.getId() + ", " + user.getName() + ", " + user.getEmail() + ", ["
					+ user.getAddress().getStreet() + ", " + user.getAddress().getCity() + ", "
					+ user.getAddress().getState() + ", " + user.getAddress().getZip() + ", "
					+ user.getAddress().getCountry() + ", " + user.getAddress().getAddressType() + "]");
		}
		System.out.println(
				"------------------------------------------------------------------------------------------------");
		request.setName("l");
		response = userPort.getUserDetails(request);
		for (User user : response.getUsers()) {
			System.out.println(user.getId() + ", " + user.getName() + ", " + user.getEmail() + ", ["
					+ user.getAddress().getStreet() + ", " + user.getAddress().getCity() + ", "
					+ user.getAddress().getState() + ", " + user.getAddress().getZip() + ", "
					+ user.getAddress().getCountry() + ", " + user.getAddress().getAddressType() + "]");
		}
	}
}

Testing the Application

Make sure first server application is running. Then run the above main class, you will get below output in the console.

3, Souvik Sanyal, souvik.sanyal@email.com, [Kalighat, Kolkata, WB, 700150, India, COMMUNICATION]
------------------------------------------------------------------------------------------------
2, Loku Poddar, debabrata.poddar@email.com, [Birati, Kolkata, WB, 700130, India, COMMUNICATION]
3, Souvik Sanyal, souvik.sanyal@email.com, [Kalighat, Kolkata, WB, 700150, India, COMMUNICATION]
4, Liton Sarkar, liton.sarkar@email.com, [Sukanta Nagar, Kolkata, WB, 700098, India, COMMUNICATION]

Source Code

You can download source code.

Thanks for reading.

1 thought on “Spring SOAP WebService Consumers using Gradle

  1. Getting error

    Some problems were found with the configuration of task ‘:wsdl2java’ (type ‘Wsdl2JavaTask’).
    – In plugin ‘no.nils.wsdl2java’ type ‘no.nils.wsdl2java.Wsdl2JavaTask’ property ‘classLoader’ is missing an input or output annotation.

Leave a Reply

Your email address will not be published. Required fields are marked *