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:
- 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.
- Use wsimport to generate the required artifacts viz. the stubs, serializers, etc.
- Write your Web services client using the generated 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.
- Create an instance of DNSLookupService
- 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.
- Now you can invoke any method in DNSLookupServiceSoap.
Here is a code snippet of how you use the DNSLookupService service:
Compile and invoke the application like so
java WSClient www.java.com
host = java.com
ip = 184.108.40.206
aliases = www.java.com
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.