Consume Any Web Service Using Eclipse + Axis
- Start Eclipse and create a new workspace named wsTest (click File -> Switch Workspace -> Other…)
- Create a new Java Project named wsTest (click File -> New -> Java Project)
- Create a new Web Service Client Proxy (click File -> New -> Other -> Web Services -> Web Service Client). In this case we are consuming a free, public web service that returns weather information for the United States. The URL for the WSDL definition is http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl.
- Create a new Java Class named Test.java (click File -> New -> Class)
- Copy and paste the following code into Test.java (overwrite the existing code):
Test.java - double click to select all 123456789101112131415161718192021import
com.cdyne.ws.WeatherWS.*;
import
java.net.URL;
public
class
Test
{
public
static
void
main(String[] arg)
{
try
{
WeatherSoapStub service = (WeatherSoapStub)
new
WeatherLocator().getWeatherSoap(
new
URL(
"http://ws.cdyne.com/WeatherWS/Weather.asmx"
));
WeatherReturn weather = service.getCityWeatherByZIP(
"47710"
);
System.out.println(
"Location: "
+ weather.getCity() +
", "
+ weather.getState());
System.out.println(
"Description: "
+ weather.getDescription());
System.out.println(
"Temperature: "
+ weather.getTemperature() +
" degrees"
);
}
catch
(Exception e)
{
e.printStackTrace();
}
}
}
Note that we set the endpoint URL on line 10. The endpoint URL is typically the same as the WSDL definition URL except the endpoint URL does not include the ?WSDL query string parameter. - Execute the code (click Run -> Run)
You should see something resembling the following output:
Location: Evansville, IN
Description: Sunny
Temperature: 49 degrees
No comments