O'Reilly logo

Java SOA Cookbook by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Calling a Web Service from the Command Line

Problem

You want a quick and simple way to call a web service that is already deployed.

Solution

Follow these basic steps:

  1. Make sure that you are running Java SE 6.

  2. Use the wsimport tool by passing it the location of your service’s WSDL. This will generate classes that conform to the WSDL messages, and convenience classes to invoke it.

  3. Create a class with a main method that will invoke the service use the generated classes.

  4. Compile and run the program.

Discussion

These steps represent an easy way to call an existing web service using JAX-WS. You can then transfer this basic idea onto more sophisticated Java projects. By doing everything from scratch, outside any IDE, you can better understand how the pieces all work. Let’s follow the steps to create the client and call a service.

Here you’ll use a service that is available publicly for free at http://WebServiceX.net. If you are testing against another service, such as one within your own organization, just replace the WSDL location string. The wsimport tool will then of course generate a different set of classes that match your WSDL, and you’ll have to put the objects together in a way that creates a meaningful message for that service. But the basic idea is always the same.

The web service you’ll invoke is described at http://www.webservicex.net/WCF/ServiceDetails.aspx?SID=44. It’s called USA Weather Forecast: you pass it a zip code, and it returns a forecast. I chose this service because while ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required