站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题Apache Axis2(java web service)备忘记

Apache Axis2(java web service)备忘记

摘要: Axis (Apache Extensible Interaction System) 为 Apache web service 开源计划, Web Service 功能为提供两个远端程式互相沟通, 其沟通使用的协定为 SOAP (Simple Object Access Protocol). Axis2 增强旧版 Axis 的效能, 加入模组化及使其更面向 XML, Axis2 设计成更容易嵌入 如 security 及其他可靠模组. 官方网站有更详细的说明.

版权声明:任何获得 Matrix 授权的网站,转载时请务必保留以下作者信息和链接
作者:joeyta(作者的blog:http://blog.matrix.org.cn/page/joeyta)
原文:http://blog.matrix.org.cn/page/joeyta?entry=apache_axis2_java_web_service
Matrix :http://www.matrix.org.cn/resource/article/44/44524_axis+soap.html
关键字:axis;soap

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 |<--SOAP| 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
下載後直接安裝到目錄 D:\tomcat
由於本人電腦安裝了幾個 tomcat , 故把 port設為8083 ( 預設port是8080 )
http://localhost:8083/ 測試是否安裝成功.

下載 axis2-std-1.0-bin.zip 並解壓到 D:\axis2
http://ws.apache.org/axis2/download.cgi
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://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/ , 畫面如下:

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

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

上面用戶及密碼為預設值,
可到 D:\tomcat\webapps\axis2\WEB-INF\conf\axis2.xml 修改 用戶名及密碼.
admin
axis2

登入後畫面如下:


安裝 Axis2 Service Archive & Code Generator Eclipse plug in(使用上發生問題):
下載 Eclipse WTP:
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
解壓至 D:\eclipse_wtp

下載 Axis2_Service_Archiver.zip 及 Axis2_Code_Generator.zip
http://ws.apache.org/axis2/tools/index.html
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
解壓後 將 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 -> New -> Java Project
Project Name: HelloWorldService -> 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 --------------------------

locked="false">test.joeyta.HelloWorldService




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

test.joeyta.HelloWorldService
描述 service class. 這裡要包含 package,

描述 此 Service 提供的服務.

Eclipse 裡的 project 如下所示:


[3] 建立 Axis2 service archive:
在 Eclipse 裡 right click "HelloWorldService" project
選 Export -> 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
設定如下圖所示:

[4] 上載 Axis2 Service:
Axis2 Administration 登陸後 選擇 Upload Service 連結:
http://localhost:8083/axis2/axis2-admin/upload
然後選擇瀏覽 D:\eclipse_wtp\workspace\HelloWorldService.aar
按 upload 後, 點選 Available Services 就會看到如下所示:


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

如果檢視 WSDL 時出現 (internal server error page) :
即檢視 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/
下載 xalan-2.7.0.jar
把它放在 D:\tomcat\webapps\axis2\WEB-INF\lib 下就能解決.


建立 Client 測試程式:
Eclise: File -> New -> 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
如下圖所示:

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>wsdl2java.bat -uri 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 , 如下圖所示.


建立 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

如下圖下示:


使用 TCP Monitor 監視 request 及 response SOAP conent:
下載舊版 Axis library axis-bin-1_4.zip:
http://apache.seekmeup.com/ws/axis/1_4/
解壓後把 axis.jar 複製至 D:\axis2\lib 裡,
進入 D:\axis2\lib 目錄
執行 D:\axis2\lib> 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 ]

修改 Client program HelloWorldServiceStub.java
將所有 port 從 8083 改為 9999 後, 再次過行 HelloWorldClient.java
就可以得到如下的 SOAP message:


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

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

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

Axis2 官方的安裝教學:
http://ws.apache.org/axis2/1_0/installationguide.html

Axis2 用戶指南:
http://ws.apache.org/axis2/1_0/userguide.html

Axis2 WEB Administration 介面的使用教學:
http://ws.apache.org/axis2/1_0/webadminguide.html

Axis2 的設定文檔:
http://ws.apache.org/axis2/1_0/axis2config.html

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

Axis2 Eclipse plugin 教學:
http://ws.apache.org/axis2/tools/1_0/eclipse/wsdl2java-plugin.html


↑返回目录
前一篇: [SOA介绍]什么是SOA?
后一篇: 整合Macromedia Flex和Java