By Alx: July 2015

Consuming web service with wsimport

Goal

This article describes how to generate classes from wsdl file (top-down approach).It needs only the wsimport tool, which is part of the jdk.

Used technologies

Jax-ws tool wsimport (JDK 8)

Generate classes with wsimport

@echo off

%JAVA_HOME%\bin\wsimport -d src -keep http://localhost:8090/ws/helloworld?wsdl


TestClient

package com.wstutorial.ws;

public class WsimportClientTest {

	public static void main(String[] args) {
		HelloWorldImplService service = new HelloWorldImplService();
		HelloWorld port = service.getHelloWorldImplPort();
		String result = port.sayHelloWorld("Good morning");
		System.out.println(result);
	}

}


Output

Hello Good morning !

Change the endpoint

Let's suppose we want to access another stage. Just change the endpoint url you want to talk with
package com.wstutorial.ws;

import java.net.MalformedURLException;
import java.net.URL;

public class WsimportClientStageTest {

	// Port 18090 instead of 8090
	public static void main(String[] args) throws MalformedURLException {
		URL endpointUrl = new URL("http://localhost:18090/ws/helloworld");
		HelloWorldImplService service = new HelloWorldImplService(endpointUrl);
		HelloWorld port = service.getHelloWorldImplPort();
		String result = port.sayHelloWorld("Good morning stage");
		System.out.println(result);
	}

}