Java Reference
In-Depth Information
Creating the client class
With a text editor, create a Java source file that puts together the objects required to make a
message that conforms to the WSDL. This is shown in Example 6-1 .
Example6-1.Client class that invokes the weather forecast service at WebServiceX.net
import net.webservicex.*;
import java.math.*;
/*
Calls the forecast service at WebServiceX.net.
*/
public class WeatherClient {
public static void main(String...arg) {
System.out.println("Invoking...");
WeatherForecast service = new WeatherForecast();
WeatherForecastSoap port = service.getWeatherForecastSoap();
//Invoke Service and Get Result
WeatherForecasts forecasts = port.getWeatherByZipCode("85255");
//Use the generated objects in the result
String placeName = forecasts.getPlaceName();
ArrayOfWeatherData arr = forecasts.getDetails();
WeatherData data = arr.getWeatherData().get(0);
System.out.println("Place=" + placeName);
System.out.println("Day=" + data.getDay());
System.out.println("High Temp (F)=" + data.getMaxTemperatureF());
System.out.println("All done.");
}
}
The client class creates an instance of the service proxy, and gets the port that has the business
method we're interested in. When you call getWeatherByZipCode , the service is invoked,
and that returns the result if everything goes well. Now all you have to do is compile and run
the client.
The best way to start putting a JAX-WS client together is to begin with the service and
work your way backwards to the individual data points. First, find the class that extends
javax.xml.ws.Service , and use it to get the appropriate port. That will tell you what type
is returned by the port. That type will be a domain object that will expose accessor (getter)
Search WWH ::




Custom Search