Please read Spring SOAP WebService Producers using Gradle before continuing this tutorial.

This tutorial will show you how we can consume SOAP based web service using Spring and Gradle. Please get the WSDL file from http://localhost:9999/ws/users.wsdl and you can save the downloaded file under src/main/resources/wsdl directory. This example shows only Spring SOAP web service consumers using Gradle to consume the already deployed or published service.

Prerequisites

Java at least version 8 needs to be installed and configured
Gradle plugin needs to be installed into Eclipse
Gradle 4.x needs to installed and configured
Dependencies : cxf

Creating and setting up Gradle project

Create gradle project called SpringBootSoapGradle using the following gradle dependencies

buildscript {
    repositories {
        mavenCentral()
    }

    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

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

Create main class

package com.jeejava.soap.client;


import com.jeejava.userservice.GetUserDetailsRequest;
import com.jeejava.userservice.GetUserDetailsResponse;
import com.jeejava.userservice.User;
import com.jeejava.userservice.UserPort;
import com.jeejava.userservice.UserPortService;

public class UserServiceClient {

                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() + "]");
                                }
                }
}

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

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

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