---
title: "Apache Axis2(java web service)備忘記(图文并茂）"
author: "duyunfengsolo"
date: "2009-08-21T14:45:30Z"
tags: ["Apache", "Eclipse", "Java", "Tomcat", "Web"]
slug: "apache-axis2-java-web-service"
draft: false
lastmod: "2009-08-21T14:45:30Z"
id: 454448
url: "https://solodu.iteye.com/blog/454448"
---

*原文地址：[joeyta](http://joeyta.blogspot.com/)*

 

Axis (Apache Extensible Interaction System) 為 Apache web service 開源計劃,  
Web Service 功能為提供兩個遠端程式互相溝通,  
其溝通使用的協定為 SOAP (Simple Object Access Protocol).

Axis2 增強舊版 Axis 的效能, 加入模組化及使其更面向 XML,  
Axis2 設計成更容易嵌入 如 security 及其他可靠模組. 官方網站有更詳細的說明.

Web service 其運作原理如下 (下圖引用官方網站簡介)：  
+--------------------------------------+  
web service registry  
(aka service broker)  
(UDDI)  
+--------------------------------------+  
^ ^

(2) (1)  
(the client (the web service  
WSDL finds the WSDL provider publishes  
service the web service)  
they want)

v v  
+-----------+ +-----------+  
service &lt;--soap--&gt; service  
requestor provider  
+-----------+ (3) +-----------+

(1) 發佈 Web Service.  
(2) 尋找已發佈的 Web Service.  
(3) 通過 SOAP 互相溝通.  
(1) 及 (2) 均為 optional.

WSDL -- Web Services description Language. WSDL 描述 web service 的功能.  
UDDI -- Universal Description, Discovery and Integration. 發佈 web service 的協定.  
SOAP -- Simple Object Access Protocol. SOAP 通過 HTTP 傳送 XML message.

下面將對Axis2 作最簡單的實作 Hello World.

**開始備忘記：**

安裝Tomcat 5.x. ( 亦可選擇使用 Axis2 提供的 SimpleHTTPServer )  
[http://apache.seekmeup.com/tomcat/tomcat-5/v5.5.17/bin/apache-tomcat-5.5.17.exe](http://apache.seekmeup.com/tomcat/tomcat-5/v5.5.17/bin/apache-tomcat-5.5.17.exe)  
下載後直接安裝到目錄 D:\\tomcat  
由於本人電腦安裝了幾個 tomcat , 故把 port設為8083 ( 預設port是8080 )  
[http://localhost:8083/](http://localhost:8083/) 測試是否安裝成功.

下載 axis2-std-1.0-bin.zip 並解壓到 D:\\axis2  
[http://ws.apache.org/axis2/download.cgi](http://ws.apache.org/axis2/download.cgi)  
[http://www.reverse.net/pub/apache/ws/axis2/1\_0/axis2-std-1.0-bin.zip](http://www.reverse.net/pub/apache/ws/axis2/1_0/axis2-std-1.0-bin.zip)

設定系統環境變數 **AXIS2\_HOME=D:\\axis2**

下載 axis2.war ：  
[http://ws.apache.org/axis2/download.cgi](http://ws.apache.org/axis2/download.cgi)  
[http://mirrors.isc.org/pub/apache/ws/axis2/1\_0/axis2.war](http://mirrors.isc.org/pub/apache/ws/axis2/1_0/axis2.war)

將 axis2.war 放進 D:\\tomcat\\webapps 目錄下.  
啟動 tomcat 就會自動產生 D:\\tomcat\\webapps\\axis2

瀏覽 [http://localhost:8083/axis2/](http://localhost:8083/axis2/) , 畫面如下：  
![](http://blog.matrix.org.cn/resources/joeyta/axis2_web1.gif)

Services -- 觀看已 desploy 的 web services.  
Validate -- 確定系統是否缺少 必要的library.  
Administration -- axis2 管理控制台.

點選 Administration 連結進入控制台登入介面, 輸入  
User ： admin  
Password : axis2

上面用戶及密碼為預設值,  
可到 D:\\tomcat\\webapps\\axis2\\WEB-INF\\conf\\axis2.xml 修改 用戶名及密碼.  
&lt;parameter locked="false" name="userName"&gt;admin&lt;/parameter&gt;  
&lt;parameter locked="false" name="password"&gt;axis2&lt;/parameter&gt;

登入後畫面如下：  
![](http://blog.matrix.org.cn/resources/joeyta/axis2_web2.gif)

**安裝 Axis2 Service Archive & Code Generator Eclipse plug in(使用上發生問題)：**  
下載 Eclipse WTP：  
[http://www.eclipse.org/webtools/](http://www.eclipse.org/webtools/)  
[http://www.eclipse.org/downloads/download.php?file=/webtools/downloads/drops/R-1.5.0-200606281455/wtp-all-in-one-sdk-R-1.5.0-200606281455-win32.zip](http://www.eclipse.org/downloads/download.php?file=%2Fwebtools%2Fdownloads%2Fdrops%2FR-1.5.0-200606281455%2Fwtp-all-in-one-sdk-R-1.5.0-200606281455-win32.zip)  
解壓至 D:\\eclipse\_wtp

下載 Axis2\_Service\_Archiver.zip 及 Axis2\_Code\_Generator.zip  
[http://ws.apache.org/axis2/tools/index.html](http://ws.apache.org/axis2/tools/index.html)  
[http://mirrors.combose.com/apache/ws/axis2/tools/1\_0/Axis2\_Service\_Archiver.zip](http://mirrors.combose.com/apache/ws/axis2/tools/1_0/Axis2_Service_Archiver.zip)  
[http://apache.edgescape.com/ws/axis2/tools/1\_0/Axis2\_Code\_Generator.zip](http://apache.edgescape.com/ws/axis2/tools/1_0/Axis2_Code_Generator.zip)  
解壓後 將 plugins 目錄複製至 D:\\eclipse\_wtp\\plugins

**測試 Axis2 Web Service：**  
編寫 Axis2 Web Service 主要需要4個步驟：  
**\[1]** 編寫實作類別.  
**\[2]** 編寫 services.xml 描述 Web Services 檔案.  
**\[3]** 建立 Web Services \*.aar archive (Axis Archive)  
**\[4]** 最後發佈 Web Services.

使用 Eclipse 建立 Project：  
Eclipse： File -&gt; New -&gt; Java Project  
Project Name： HelloWorldService -&gt; Finish

**\[1]** 建立 HelloWorldService class：  
\-------------- HelloWorldService.java -----------------  
package test.joeyta;  
public class HelloWorldService {  
public String echo(String value) { // 這個 service 只是將輸入的文字 return 出去  
System.out.println("Service HelloWorldService: " + value);  
return value;  
}  
}  
\-------------- HelloWorldService.java -----------------

**\[2]** 新增目錄 D:\\eclipse\_wtp\\HelloWorldService\\META-INF  
建立 service descriptor 檔案 D:\\eclipse\_wtp\\HelloWorldService\\META-INF\\services.xml：  
\--------------------- services.xml --------------------------

 

xml 代码

 

1. &lt;service&gt;
2. &lt;parameter name="ServiceClass"
3. locked="false"&gt;test.joeyta.HelloWorldService&lt;/parameter&gt;
4. &lt;operation name="echo"&gt;
5. &lt;messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/&gt;
6. &lt;/operation&gt;
7. &lt;/service&gt;

--------------------- services.xml --------------------------

 

&lt;parameter locked="false" name="ServiceClass"&gt;test.joeyta.HelloWorldService&lt;/parameter&gt;  
描述 service class. 這裡要包含 package,  
&lt;operation name="echo"&gt;  
描述 此 Service 提供的服務.&lt;/operation&gt;

Eclipse 裡的 project 如下所示：  
![](http://blog.matrix.org.cn/resources/joeyta/axis2_eclpse1.gif)

**\[3]** 建立 Axis2 service archive：  
在 Eclipse 裡 right click "HelloWorldService" project  
選 Export -&gt; JAR file  
Select the resource to export: 點擇 HelloWorldService 裡的 services.xml 及 test.joeyta package  
JAR file： D:\\eclipse\_wtp\\workspace\\HelloWorldService.aar  
然後按 Finish, 就會產生 D:\\eclipse\_wtp\\workspace\\HelloWorldService.aar  
設定如下圖所示：  
![](http://blog.matrix.org.cn/resources/joeyta/axis2_eclpse2.gif)

**\[4]** 上載 Axis2 Service：  
Axis2 Administration 登陸後 選擇 Upload Service 連結：  
[http://localhost:8083/axis2/axis2-admin/upload](http://localhost:8083/axis2/axis2-admin/upload)  
然後選擇瀏覽 D:\\eclipse\_wtp\\workspace\\HelloWorldService.aar  
按 upload 後, 點選 Available Services 就會看到如下所示：  
![](http://blog.matrix.org.cn/resources/joeyta/axis2_web3.gif)

在上圖中點選 HelloWorldService 連結就會看到該 service 的 WSDL：  
[http://localhost:8083/axis2/services/HelloWorldService?wsdl](http://localhost:8083/axis2/services/HelloWorldService?wsdl)

如果檢視 WSDL 時出現 (internal server error page) ：  
即檢視 WSDL： [http://localhost:8083/axis2/services/HelloWorldService?wsdl](http://localhost:8083/axis2/services/HelloWorldService?wsdl)  
出現 Provider org.apache.xalan.processor.TransformerFactoryImpl not found 等信息.  
請到 [http://www.apache.org/dist/java-repository/xalan/jars/](http://www.apache.org/dist/java-repository/xalan/jars/)  
下載 xalan-2.7.0.jar  
把它放在 D:\\tomcat\\webapps\\axis2\\WEB-INF\\lib 下就能解決.

**建立 Client 測試程式：**  
Eclise： File -&gt; New -&gt; Java Project  
Project name： HelloWorldClient  
按 Finish 就產生 HelloWorldClient project  
點擊 HelloWorldClient 右鍵選 Properties  
然後選擇 Java Build Path,  
Source：  
Source folders on build path: 新增 src 目錄  
Default output folder: HelloWorldClient/bin  
如下圖所示：  
![](http://blog.matrix.org.cn/resources/joeyta/axis2_eclpse4.gif)  
Library:  
按 Add External JARs 新增所有 D:\\axis2\\lib 裡的 jar libraries.

使用 WSDL2JAVA 產生 stub classes：  
由於測試時 Eclipse Axis2 Code Generator plugin 出問題,  
故只好在 DOS command prompt 用手工輸入產生.

進入目錄 D:\\axis2\\bin , 執行：  
D:\\axis2\\bin&gt;wsdl2java.bat -uri [http://localhost:8083/axis2/services/HelloWorldService?wsdl](http://localhost:8083/axis2/services/HelloWorldService?wsdl) -o D:\\eclipse\_wtp\\workspace\\HelloWorldClient -p test.joeyta  
\-uri WSDL 的 uri 位置.  
\-o 輸出 stub classes 的位置. 預設會增加 src 目錄.  
\-p 設定輸出 stub classes 使用的 package.

產生 HelloWorldServiceStub.java 及 HelloWorldServiceCallbackHandler.java , 如下圖所示.  
![](http://blog.matrix.org.cn/resources/joeyta/axis2_eclpse3.gif)

建立 Client 程式  
\------------------- HelloWorldClient.java ----------------------  
public class HelloWorldClient {

public static void main(String\[] args) throws Exception {

HelloWorldServiceStub stub = new HelloWorldServiceStub();

HelloWorldServiceStub.Echo request = new HelloWorldServiceStub.Echo();  
request.setValue("Hello world, Joeyta");

EchoResponse response = stub.echo(request);

System.out.println("Response : " + response.get\_return());  
}

}  
\------------------- HelloWorldClient.java ----------------------

執行後輸出結果：  
Response : Hello world, Joeyta

如下圖下示：  
![](http://blog.matrix.org.cn/resources/joeyta/axis2_eclpse5.gif)

**使用 TCP Monitor 監視 request 及 response SOAP conent：**  
下載舊版 Axis library axis-bin-1\_4.zip：  
[http://apache.seekmeup.com/ws/axis/1\_4/](http://apache.seekmeup.com/ws/axis/1_4/)  
解壓後把 axis.jar 複製至 D:\\axis2\\lib 裡,  
進入 D:\\axis2\\lib 目錄  
執行 D:\\axis2\\lib&gt; java -classpath axis.jar org.apache.axis.utils.tcpmon  
就會彈出 TCPMonitor  
Listen port 設為 9999 ,  
Target Hostname ： 127.0.0.1  
Target Port：8083  
然後按 Add, 如下圖所示：  
\[ 這樣設可以依靠 port 9999 去監聽 port 8083 ]  
![](http://blog.matrix.org.cn/resources/joeyta/axis2_tcpmon1.gif)  
修改 Client program HelloWorldServiceStub.java  
將所有 port 從 8083 改為 9999 後, 再次過行 HelloWorldClient.java  
就可以得到如下的 SOAP message：  
![](http://blog.matrix.org.cn/resources/joeyta/axis2_tcpmon2.gif)

使用 SOAP Monitor 監視 SOAP data, 下面是官方的教學.  
[http://ws.apache.org/axis2/1\_0/soapmonitor-module.html](http://ws.apache.org/axis2/1_0/soapmonitor-module.html)

官方文檔非常多及齊全, 若想進一步了解, 需要花更長的時間：  
[http://ws.apache.org/axis2/1\_0/index.html](http://ws.apache.org/axis2/1_0/index.html)

Axis2 的文檔及教學非常充足. 官方文檔如下連結：  
[http://ws.apache.org/axis2/1\_0/index.html](http://ws.apache.org/axis2/1_0/index.html)

Axis2 官方的安裝教學：  
[http://ws.apache.org/axis2/1\_0/installationguide.html](http://ws.apache.org/axis2/1_0/installationguide.html)

Axis2 用戶指南：  
[http://ws.apache.org/axis2/1\_0/userguide.html](http://ws.apache.org/axis2/1_0/userguide.html)

Axis2 WEB Administration 介面的使用教學：  
[http://ws.apache.org/axis2/1\_0/webadminguide.html](http://ws.apache.org/axis2/1_0/webadminguide.html)

Axis2 的設定文檔：  
[http://ws.apache.org/axis2/1\_0/axis2config.html](http://ws.apache.org/axis2/1_0/axis2config.html)

AXIOM (Axis Object Model) 教學：  
[http://ws.apache.org/axis2/1\_0/OMTutorial.html](http://ws.apache.org/axis2/1_0/OMTutorial.html)

Axis2 Eclipse plugin 教學：  
[http://ws.apache.org/axis2/tools/1\_0/eclipse/wsdl2java-plugin.html](http://ws.apache.org/axis2/tools/1_0/eclipse/wsdl2java-plugin.html)