首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Axis2 整合 Eclipse 開發 Web Service 入門:計算器服務完整實戰

     編輯:Java 相關知識

使用 Eclipse JEE + Axis2 1.4.1 快速建立 SOAP Web Service,從環境配置、服務實現、發布測試到客戶端驗證完整流程。

環境準備

  1. Eclipse IDE for Java EE Developers
  2. Axis2 1.4.1 Standard Binary Distribution (axis2-1.4.1-bin.zip)
  3. 解壓至任意目錄,記住路徑

Eclipse 配置 Axis2 Runtime

  1. Window → Preferences → Web Services → Axis2 Preferences
  2. Axis2 runtime location 選擇解壓目錄
  3. Apply & Close

Axis2 目錄結構 Eclipse Axis2 Preferences

建立計算器服務

1. 新建 Java Project: WS_01

2. 建立服務實現類

package rong.service;

public class CalculateService {
  public float plus(float x, float y) { return x + y; }
  public float minus(float x, float y) { return x - y; }
  public float multiply(float x, float y) { return x * y; }
  public float divide(float x, float y) { return x / y; }
}

3. 產生 Web Service (Wizard)

  1. 專案右鍵 → New → Other → Web Services → Web Service
  2. Service implementation: Browse 選擇 CalculateService
  3. Web Service type 滑桿 → Start service (發布服務)
  4. Client type 滑桿 → Test client (產生測試客戶端)
  5. Configuration (Server):Tomcat 6.0 + Apache Axis2
  6. Configuration (Client):同樣配置
  7. Next → Next → Start Server (啟動 Tomcat)
  8. Finish

Web Service Wizard 選擇服務類 滑桿配置 Server/Client Configuration 啟動 Tomcat

測試驗證

完成後自動開啟 Web Services Explorer:

  1. 選擇操作 (如 `plus`)
  2. 輸入參數: x=2, y=3
  3. 點擊 Go
  4. Status 欄顯示結果: 5.0

Web Services Explorer 測試 plus 操作

生成檔案結構說明

  • `services.xml`:服務部署描述 (Axis2 特有)
  • `wsdl` 目錄:WSDL 合約檔
  • 客戶端 Stub:`CalculateServiceStub.java` 等
  • 建構腳本:`build.xml` (Ant) / `.classpath` `.project`

進階擴展方向

  • 複雜資料類型:自定義 Java Bean 自動映射為 XML Schema
  • 安全性:WS-Security (UsernameToken, Signature, Encryption)
  • 非同步調用:`CallbackHandler` / `Future` 模式
  • REST 風格:Axis2 亦支援 RESTful 服務發布
  • Maven 整合:`axis2-maven-plugin` 自動化建構

Axis2 + Eclipse Wizard 讓 Web Service 開發「所見即所得」,適合快速原型與教學驗證。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved