Site icon Soul & Shell Blog

Apache AXIS 2 - SOAP Web Service 實作教學 (即時天氣預報)

Apache AXIS 2 官方網站

下載與安裝AXIS

解壓縮

測試 WSDL

1.上網找一個免費的 Web Service

這是全球天氣Web Service

2.取得 WSDL 描述文件

3.透過 Apache AXIS 2 產生 Java Class

執行:

java -classpath .\lib\axis.jar;.\lib\jaxrpc.jar;.\lib\commons-logging-1.0.4.jar;.\lib\commons-discovery-0.2.jar;.\lib\wsdl4j-1.5.1.jar org.apache.axis.wsdl.WSDL2Java -p com.slab.soap.weather http://www.webservicex.net/globalweather.asmx?WSDL

-p 代表產生的package名稱
執行後會在目錄中產生Java Source Code
執行結果如下圖

4.寫一支測試程式

查詢台灣高雄國際機場的天氣

import com.slab.soap.weather.GlobalWeather;
import com.slab.soap.weather.GlobalWeatherLocator;
import com.slab.soap.weather.GlobalWeatherSoap_PortType;

public class RunService {

    public static void main(String []args) throws Exception {
        GlobalWeather service = new GlobalWeatherLocator();
        GlobalWeatherSoap_PortType client = service.getGlobalWeatherSoap();
        System.out.println(client.getWeather("Kaohsiung International Airport", "Taiwan"));
    }

}

5.執行測試

執行需要的 Library Jar File
lib/axis.jar,
lib/jaxrpc.jar,
lib/commons-discovery-0.2.jar,
lib/wsdl4j-1.5.1.jar

結果輸出

<?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>Kaohsiung International Airport, Taiwan (RCKH) 22-35N 120-21E</Location>
<Time>Jul 17, 2009 - 02:30 AM EDT / 2009.07.17 0630 UTC</Time>
<Wind> from the W (270 degrees) at 12 MPH (10 KT):0</Wind>
<Visibility> greater than 7 mile(s):0</Visibility>
<SkyConditions> partly cloudy</SkyConditions>
<Temperature> 87 F (31 C)</Temperature>
<DewPoint> 78 F (26 C)</DewPoint>
<RelativeHumidity> 74%</RelativeHumidity>
<Pressure> 29.47 in. Hg (0998 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather>

Exit mobile version