设为首页 收藏本站
查看: 753|回复: 0

[经验分享] Apache Axis2(java web service)備忘記(图文并茂)(转)

[复制链接]

尚未签到

发表于 2017-1-13 07:37:22 | 显示全部楼层 |阅读模式
原文地址:joeyta  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/ , 畫面如下:
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 修改 用戶名及密碼.
<parameter locked="false" name="userName">admin</parameter>
<parameter locked="false" name="password">axis2</parameter>

  登入後畫面如下:
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/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 --------------------------
  
xml 代码
 

  • <service>  
  • <parameter name="ServiceClass"  
  • locked="false">test.joeyta.HelloWorldService</parameter>  
  • <operation name="echo">  
  • <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>  
  • </operation>  
  • </service>  



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

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

  Eclipse 裡的 project 如下所示:
http://blog.matrix.org.cn/resources/joeyta/axis2_eclpse1.gif

[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
設定如下圖所示:
http://blog.matrix.org.cn/resources/joeyta/axis2_eclpse2.gif

  [4] 上載 Axis2 Service:
Axis2 Administration 登陸後 選擇 Upload Service 連結:
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

  如果檢視 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
如下圖所示:
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>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 , 如下圖所示.
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/
解壓後把 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 ]
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/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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-327643-1-1.html 上篇帖子: 为Weblogic9.2安装和配置Apache HTTP 服务器插件 下篇帖子: 基于mod_proxy+Apache 2.2.16+Tomcat 7的负载均衡与集群配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表