標籤:http io 使用 java ar strong for 檔案 sp
用戶端開發步驟(java):
本例展示如何在Eclipse 環境下使用Axis2建立WebService用戶端。
一、 環境準備(如果你已具備Axis2開發環境,跳過本步驟)
1、 Axis2 API包
本例使用的Axis2版本是1.4,請到以下地址下載:
http://apache.mirror.phpchina.com/ws/axis2/1_4/axis2-1.4.1-bin.zip
下載後將壓縮包解壓縮到任意目錄。如果你希望直接使用Axis2 API進行開發,可能會需要這個包。
2、 Axis2 Eclipse 外掛程式( 代碼產生和打包工具 )
本例使用的外掛程式版本是1.4,請到以下地址下載:
打包工具:
http://apache.mirror.phpchina.com/ws/axis2/tools/1_4/axis2-eclipse-service-archiver-wizard-1.4.zip
代碼產生工具:
http://apache.mirror.phpchina.com/ws/axis2/tools/1_4/axis2-eclipse-codegen-wizard-1.4.zip
其實我們只用到代碼產生工具。
3、 安裝Axis2 Eclipse外掛程式
(1) 將下載到的 Axis2 的兩個外掛程式解壓縮到 Eclipse 安裝目錄下的 plug-in 子目錄;
(2) 到Axis2 API安裝目錄的/lib目錄下,複製backport-util-concurrent-3.1.jar和geronimo-stax-api_1.0_spec-1.0.1.jar這兩個檔案至eclipse安裝目錄的/plugins/Axis2_Codegen_Wizard/lib目錄下,同時要編輯eclipse安裝目錄的/plugins/Axis2_Codegen_Wizard下的plugin.xml, 將這兩個檔案添加進去(紅色的兩項):
<library name="lib/jibx-run-1.1.5.jar">
<export name="*"/>
</library>
<library name="lib/backport-util-concurrent-3.1.jar">
<export name="*"/>
</library>
<library name="lib/geronimo-stax-api_1.0_spec-1.0.1.jar">
<export name="*"/>
</library>
</runtime>
然後啟動 Eclipse ,並選擇“ File->New->Other ”可以找到下面兩個 Wizards:
Axis2 Code Generator和Axis2 Service Archiver,表明外掛程式安裝成功。
二、開發用戶端
開啟Eclipse,建立java項目,JDK最好選擇JDK1.5。
1、 選擇“Build Classpath à Add Libraries …”,把Axis2 API安裝目錄 /lib 目錄下所有 jar 包添加進來。
2、建立一個package,例如com.yourcompany.client。
3、使用“File à New à Other ”,在選擇嚮導視窗中選擇Axis2 Wizard下的Axis2 Code Generator外掛程式。 Next。
4、 選擇“Generate java source code from a WSDL file”,Next。
5、在WSDL file location中填入WSDL訪問地址,例如:http://192.168.0.1/axis2/services/xxx?wsdl。Next。
6、Codegen option選擇 default。Next。
7、選擇輸出路徑,點中“Browse and select a project on current eclipse workspace”,然後點擊“Browse…”按鈕,選擇當前項目。Finish,Codegen外掛程式產生用戶端代理代碼完畢
8、建立測試類別,調用WebService 介面方法。執行個體代碼:
public class OrderServiceClient {
public static void main(String[] args)throws AxisFault{
OrderServiceStub stub = new OrderServiceStub();
//測試按手機號尋找訂購資訊
OrderServiceStub.GetOrderInfoDFByPhone request = new OrderServiceStub.GetOrderInfoDFByPhone();
request.setPhone("15969000000");
OrderInfoDF[] result=null;
try{
result=stub.getOrderInfoDFByPhone(request).get_return();
}catch(Exception e){
e.printStackTrace();
}
if(result!=null){
for(int i=0;i<result.length;i++){
OrderInfoDF o=result[i];
System.out.println("###################");
System.out.println("區號:"+o.getAreaid());
System.out.println("戶號:"+o.getCustomerid());
System.out.println("訂購狀態:"+o.getLastsendflag());
System.out.println("最後發送時間:"+o.getLastsendtime());
System.out.println("訂購指令:"+o.getOrderaction());
System.out.println("訂購時間:"+o.getOrdertime());
System.out.println("手機號:"+o.getPhone());
System.out.println("姓名:"+o.getUsername());
}
}
}
運行結果如下:
###################
區號:0871
戶號:30200363
訂購狀態:0
最後發送時間:2008-12-29 15:39:38
訂購指令:DF#30200363#0871
訂購時間:2008-07-30 15:07:46
手機號:15969000000
姓名:null
###################
WebService 介面調用指南