×
☰ See All Chapters

SOAP service client example

In this tutorial we will learn to create web service client or consumer. We can use “wsimport” tool to parse the published wsdl file, and generate necessary client files(stub) to access the published web service. Follow the below steps to create the service client by executing wsimport against the published web service.

Note: Before working with JAX-WS client make sure there is a web services running.

Steps to create JAX-WS web service client.

Step 1: Create a java project in eclipse.

soap-service-client-0
 

Step 2: Open command prompt, and navigate to our project root folder (shift+right click on the project root folder and click on “Open command window here” option)

Issue the following command, to generate client side artifacts.

wsimport -d src -keep -verbose https://localhost:7779/ws/hello?wsdl

soap-service-client-1
 

Step 3:  Now go to our project and refresh the project, we can find the client side artifacts as follows.

 soap-service-client-2
 

Step 4: Now write the client application.

soap-service-client-3
 

JaxWsClient.java

package com.java4coding;

 

public class JaxWsClient {

        public static void main(String[] args) {

                HelloWorldImplService implService = new HelloWorldImplService();

                HelloWorld helloWorld = implService.getHelloWorldImplPort();

                System.out.println( helloWorld.getHelloWorldAsString("Manu M"));

        }

}

Output: Run JaxWsClient.java

soap-service-client-4
 

Step 6: Now we can write dynamic client(Without client side proxies) as follows…

JaxWsDynamicClient.java

package com.java4coding;

import java.net.MalformedURLException;

import java.net.URL;  

import javax.xml.namespace.QName;  

import javax.xml.ws.Service;

public class JaxWsDynamicClient {

        public static void main(String[] args) {

                URL url;

                try {

                        url = new URL("https://localhost:7779/ws/hello?wsdl");

                        // 1st argument service URI, refer to wsdl document above

                        // 2nd argument is service name, refer to wsdl document above

                        QName qname = new QName("https://java4coding.com/", "HelloWorldImplService");

                        Service service = Service.create(url, qname);

                        HelloWorld hello = service.getPort(HelloWorld.class);

                        System.out.println(hello.getHelloWorldAsString("Manu M"));

                } catch (MalformedURLException e) {

                        e.printStackTrace();

                }  

        }

}

JaxWsAnnotationClient.java

package com.java4coding;

import javax.xml.ws.WebServiceRef;

public class JaxWsAnnotationClient {

        @WebServiceRef(wsdlLocation="https://localhost:8080/helloservice/hello?wsdl")

        static HelloWorldImplService implService;

        public static void main(String[] args) {

                HelloWorld helloWorld = implService.getHelloWorldImplPort();

                System.out.println( helloWorld.getHelloWorldAsString("Manu M"));

        }

}

In order for @WebServiceRef annotations to work on the client side it needs an IoC container that's capable of providing the injection. Otherwise we get java.lang.NullPointerException.

 


All Chapters
Author