Consuming Web services

Lee Chuk-Munn shows how easy it is to use Mustang, or JavaSE 6, to consume a Web service.

Web services is all the rage. Web services holds the promise of being able to transform software to services. The Java platform, Java Enterprise Edition (JavaEE) in particular, is a popular choice for developing and deploying Web services. However, if you are developing on Java Standard Edition application (JavaSE), consuming or deploying Web services is not as easy or as straightforward until JavaSE 6 (aka Mustang).

In this article, we will look at how easy it is to use Mustang to consume a Web service. Here are the steps you have to perform:

  1. Determine the Web service you are going to interact with. XMethods has a fairly good list of Web services for you to try. Select any Web service.
  2. Use wsimport to generate the required artifacts viz. the stubs, serializers, etc.
  3. Write your Web services client using the generated artifacts.
Generating Web Services Artifacts
For discussion purposes, we will consume a Web service, hosted on the Microsoft platform that provides DNS like information. The WSDL document of that service can be found here:

We now use the wsimport to generate the Web service artifacts:

wsimport -s src -p

The -p specifies the package name of the generated artifacts and -s is the directory name to keep the generated artifact sources. You do not have to keep the source if the Web service that you are dealing with returns simple values like String or integers. But if the service is returning complex objects, like DNSInfo in our case, then we need to look at these classes to figure out how to get the values. You will probably also need to look at the source to find out what are the Web services you can invoke if you are not familiar with it.

For every service in the WSDL document, a corresponding class will be generated. This is also true for ports within a service. In dnslookupservice WSDL document, we find one service called DNSLookupService and within this service is a port called DNSLookupServiceSoap. So there will be a corresponding DNSLookupService.class and DNSLookupServiceSoap.class respectively that is generated by wsimport.

Consuming Web Services
Now that we have the Web service artifacts, we can now use these artifacts to invoke the Web service. Here are the steps to call DNSLookupService.

  1. Create an instance of DNSLookupService
  2. For every port inside this service, there is a corresponding method called get<Port Name>. This method will return the instance of the port. In our case, if we invoke getDNSLookupServiceSoap, we will get a reference to DNSLookupServiceSoap object.
  3. Now you can invoke any method in DNSLookupServiceSoap.

Here is a code snippet of how you use the DNSLookupService service:

import wsclient.*;

public class WSClient {

    public static void main(String[] args) throws Exception {
        //Create an instance of the DNSLookupService
        DNSLookupService webservice = new DNSLookupService();
        //Get a reference to the port
        DNSLookupServiceSoap service = webservice.getDNSLookupServiceSoap();
        for (String address: args) {
        //Invoke the web service
            DNSInfo info = service.getDNSInfoByWebAddress(address);
        //Print out the result
          System.out.println("host = " + info.getHost());
            for (Object obj: info.getIPAddress().getAnyType())
                System.out.println("\tip = " + obj);
            for (Object obj: info.getAliases().getAnyType())
                System.out.println("\taliases = " + obj);

Compile and invoke the application like so

    java WSClient
    host =
        ip =
        aliases =

Lee Chuk-Munn has been programming in the Java language since 1996, when he first joined Sun Microsystems in Hong Kong. He currently works as a senior developer consultant and technology evangelist for Technology Outreach at Sun in Singapore. Chuk's focus is in Java APIs, Java EE, Java SE, and Java ME. Chuk graduated in 1987 from the Royal Melbourne Institute of Technology in Melbourne, Australia, where his favorite subject was compiler theory.