Consume jax-ws webservice

Introduction

In this tutorial I will show how to create the Webservice client for JAX-WS webservice. We will consume same webservice which we created in the example. We will use here gradle tool to generate client or stub from the SOAP webservice or JAX-WS webservice.

A service has already been deployed into the server and we are given a wsdl file we can invoke the service from the remote area.

Prerequisites

Eclipse 4.12, JDK 12, Gradle 5.6, wsdl2java plugin

Read before you proceed jax-ws webservice creating tutorial

Creating Project

Create a gradle based project in Eclipse. The project name is jax-ws-webservice-tomcat-consume.

Updating Build File

We need to update the build file for including required dependencies in build.gradle file.

plugins {
    id 'java-library'
    id "com.github.bjornvester.wsdl2java" version "0.2"
}
sourceCompatibility = 12
targetCompatibility = 12
repositories {
    mavenCentral()
    maven {
		url 'http://maven.java.net/content/repositories/staging/'
	}
}
dependencies {
	implementation 'com.sun.xml.ws:rt:2.3.1'
	implementation 'org.glassfish.jaxb:txw2:2.4.0-b180608.0325'
}
wsdl2java {
	cxfVersion.set("3.3.2")
}

WSDl File

Download the WSDL file from the example and save it as hello.wsdl under src/main/resources folder of our project.

<?xml version='1.0' encoding='UTF-8'?><!-- Published by JAX-WS RI (http://jax-ws.java.net).
	RI's version is JAX-WS RI 2.3.1 svn-revision#6ef5f7eb9a938dbc4562f25f8fa0b67cc4ff2dbb. --><!-- Generated by JAX-WS RI (http://javaee.github.io/metro-jax-ws).
	RI's version is JAX-WS RI 2.3.1 svn-revision#6ef5f7eb9a938dbc4562f25f8fa0b67cc4ff2dbb. -->
<definitions
	xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
	xmlns:wsp="http://www.w3.org/ns/ws-policy"
	xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
	xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata"
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://impl.service.ws.jax.roytuts.com/"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns="http://schemas.xmlsoap.org/wsdl/"
	targetNamespace="http://impl.service.ws.jax.roytuts.com/"
	name="HelloImplService">
	<import namespace="http://service.ws.jax.roytuts.com/"
		location="http://localhost:8080/jax-ws-webservice-tomcat/hello?wsdl=1" />
	<binding xmlns:ns1="http://service.ws.jax.roytuts.com/"
		name="HelloImplPortBinding" type="ns1:Hello">
		<soap:binding
			transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
		<operation name="sayHello">
			<soap:operation soapAction="" />
			<input>
				<soap:body use="literal"
					namespace="http://service.ws.jax.roytuts.com/" />
			</input>
			<output>
				<soap:body use="literal"
					namespace="http://service.ws.jax.roytuts.com/" />
			</output>
		</operation>
	</binding>
	<service name="HelloImplService">
		<port name="HelloImplPort" binding="tns:HelloImplPortBinding">
			<soap:address
				location="http://localhost:8080/jax-ws-webservice-tomcat/hello" />
		</port>
	</service>
</definitions>

Generating Stub

Now build the project using command line tool by executing gradle build and you will find the generated Java classes under build/generated/wsdl2java folder.

Creating Client Class

Now create a main class which will be used for invoking the service.

The service invoking process in the main class is given below.

package com.roytuts.jax.ws.service.consumer;
import com.roytuts.jax.ws.service.Hello;
import com.roytuts.jax.ws.service.impl.HelloImplService;
public class HelloClient {
	public static void main(String[] args) throws Exception {
		HelloImplService service = new HelloImplService();
		Hello hello = service.getHelloImplPort();
		System.out.println(hello.sayHello("Soumitra"));
	}
}

Testing the Application

By executing the above main class you will get the response as Hello Soumitra.

Source Code

Download Source Code

Thanks for reading.

Leave a Reply

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