Web Service Clients  Locate

Overview  Locate

This chapter shows how to create and run a client for a Web service. This process is also known as 'invoking' a Web service. It uses the Web service implemented in the chapter Writing Web Services and published to the server in Publishing. Refer to these chapters for the steps preceding invocation.

We have a number of ways to invoke a Web service. The most simple and straightforward is synchronous invocation via dynamic proxy, which is shown here. For other types of invocation, both synchronous and asynchronous, see the Web Service Clients book.

Generating Service Interfaces  Locate

Start the embedded server (HelloServer from Publishing) or the standalone server with the deployed HelloServiceImpl as described in Single-Class Persistent Deployment. The Java service interface is generated using the WSDL2Java tool. Given a service placed at http://localhost:6060/HelloService/wsdl, the service interface is generated using:

WSDL2Java --interface-package example.basics.invocation --url http://localhost:6060/HelloService/wsdl

More information about the WSDL2Java tool and its parameters can be found in the chapter WSDL2Java.

[Note]Note

You may notice that WSDL2Java also generates a HelloServiceImpl.xmap. You need not concern yourself with this file at this time. The .xmap file is used in certain cases when the Web service is invoked on the client. For more information, see Mapping Files in Java Clients, Invoking Web Services.

The generated interface is shown in Example 4. By default, this interface includes both synchronous and asynchronous methods. We will use only synchronous invocation in this chapter. For more information, see Asynchronous Invocation.

Example 4. Generated HelloService Interface

// Copyright WSO2 Inc. All rights reserved.
// Use is subject to license terms.
package example.basics.invocation;

import org.systinet.wasp.async.AsyncConversation;


public interface HelloService {
  // synchronous invocation of 'hello'
  String hello(String message);

  // asynchronous invocation of 'hello'
  AsyncConversation beginHello(String message);

  String endHello(AsyncConversation conversation);
}

Creating the Client  Locate

We create a simple client for invoking the Web service. This client creates a dynamic proxy for the Web service during the lookup operation in the following line of code:

HelloService
      proxy=(HelloService)Registry.lookup(WSDL_URL,HelloService.class);

This is shown in Example 5.

Example 5. Simple Client

// Copyright WSO2 Inc. All rights reserved.
// Use is subject to license terms.
package example.basics.invocation;

import org.systinet.wasp.webservice.Registry;


public class HelloClient {
  private static final String WSDL_URL = "http://localhost:6060/" + 
                                         "HelloService/wsdl";

  public static void main(String[] args) throws Exception {
    HelloService proxy = (HelloService) Registry.lookup(WSDL_URL,
        HelloService.class);
    System.out.println(proxy.hello("world"));
  }
}

In Example 5 we created a dynamic proxy for the Web service from http://localhost:6060/HelloService/wsdl, calling the hello method and printing the return string from the Web service on standard output.

Compile and run the client together with the generated service interface. (If doing this from the command prompt, make sure the environment is set correctly.)

Running a Client with SOAPSpy  Locate

If you wish to follow the progress of the invocation with SoapSpy, perform the following:

  1. Run server_java6.5.4/bin/soapspy.bat or soapspy.sh

  2. Start spying by selecting Start Spying from the Spy menu or by clicking the spy icon in the main pane

  3. Run the client using the run spy_client command.

Please see Soap Spy for more information.

[Note]Note

The Ctrl-C key combination for copying text does not work in SOAPSpy. Use Ctrl-Insert instead.