網頁

2012年3月31日 星期六

Web Service - Axis2

Last Update: 2012/04/01 07:28+08
Type: Normal

Intro

target 是在 用 Axis2 建立 Web Service 及其 Client


環境:
jdk-1.7.0.3
Axis2-1.6.1
eclipse-jee-indigo-SR2-win32
JBoss 7.1 (not important, you can use tomcat)

P.S. 若用 JBoss 7.1
downlaod 官網的 JBoss Server 後
請先用 eclipse market 找 JBoss 然後安裝適合你的(eclipse)版本
不然會找不到 7.1 的 Server


Content


.. 安裝必要 Component
(路徑自選)
安裝 jdk 到 C:\Program Files\Java\jdk1.7.0_03
解壓縮 eclipse, JBoss and Axi2 到 D:\ProgramData
啟動eclipse~

.. 安裝JBoss plugin 用以支援6.0以上
(選單)Help => Eclipse Marketplace => 搜尋 JBoss 並安裝

咱的eclipse版本是 Indigo(3.7), 所以 JBoss plugin tool 是 3.3
在 Marketplace 選擇有標記 Indigo 的

JBoss 官網 Download


.. 設定執行環境 - JDK
(選單) Window => Preferences => Java => Complier => Compiler compliiance leve: 1.7(自選)

(選單) Window => Preferences => Java => Installed JREs => 新增JDK 1.7 (自選- C:\Program Files\Java\jdk1.7.0_01)

.. 設定執行環境 - JBoss
(選單) Window => Preferences => Sreve => Runtime Environments
=> 新增 JBoss 7.1 (自選- D:\ProgramData\jboss-as-7.1.1.Final)

.. 設定執行環境 - Axis2
(選單) Window => Preferences => Web Services => Axis2 Preferences
=> 在 [Axis2 runtime location] 設定 Axis2 路徑(自選- D:\ProgramData\axis2-1.6.1)

.. 建立 Web Service
---Create Project---
按 Ctrl + N => 選 Web Service => 下一步


... Configuration
Server Runtime: JBoss AS 7.1
Web Service Runtime: Axis2
Service Project: ExampleService
Service EAR project: ExampleEAR


... Web Service Project 會包含 Axis2 的 Web Site

... 必需先加一個 Service (com.example.service.User)
之後可以再加

... 此專案可包含多個 service
所以我就命名ExampleService
底下再放其它的 ex: User, Order, ...

... EAR project 是必選 囧
就乾脆叫 ExampleEAR
到時連同 client 一起加進來部署

...next 後, 會產生必要的檔案
再選擇你自己的 services.xml 或 讓他自動產生, 結果


... services.xml 預設內容
有自動設定 mapping 到 com.example.service.User
但沒產生那個class... XD
<service name="User" >
 <Description>
  Please Type your service description here
 </Description>
 <messageReceivers>
  <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
  <messageReceiver  mep="http://www.w3.org/2004/08/wsdl/in-out"  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
 </messageReceivers>
 <parameter name="ServiceClass" locked="false">com.example.service.User</parameter>
</service>

---建立Service---
自己新增 com.example.service.User class
package com.example.service;
public class User {
 public String sayHello(String name) {
  return "Hello, " + name;
 }
}
在 services.xml 裡加入 operation (位置: ./service/opertaion )
<operation name="sayHello"></operation>

... 完成!!! 啟動Server (Create Proejct 時 產生的)


...連接到 http://localhost:8080/ExampleService/
可以看到 axis2 的管理介面
點 Services => User 可以看到你的 wsdl

.. 建立 Web Service Client
---Create Project---
... Ctrl + N => 建立 Web Service Client

... 在 Service definition 貼上你的 wsdl 路徑
ex: http://localhost:8080/ExampleService/services/User?wsdl

... Configuration
Server Runtime: JBoss AS 7.1
Web Service Runtime: Axis2
Service Project: ExampleWeb
Service EAR project: ExampleEAR


... 其它 - 預設

... 會產生 UserCallbackHandler.java 和 UserStub.java

注意!! 這似乎是用到舊版的產生器
所以, 咱們不要它,


---產生Stub(Skeletion)---
在build.xml(ant)中 加入
<target name="wsdl2java">
    <path id="axis2.jars">
        <fileset dir="D:/ProgramData/axis2-1.6.1/lib/">
            <include name="**/*.jar"/>
        </fileset>
    </path>
    <taskdef name="axis2-wsdl2java" classname="org.apache.axis2.tool.ant.AntCodegenTask">
     <classpath refid="axis2.jars"/>
    </taskdef>
    <axis2-wsdl2java wsdlfilename="http://localhost:8080/ExampleService/services/User?wsdl" output="./" skipbuildxml="true" />
</target>
然後對 build.xml 點右鍵 => Run As => Ant Build...
=> 勾選 wsdl2java => Run
這會再產生 Handler 和 Stub

或參考其它wsdl2java 產生方法


---使用Service---
UserStub stub = new UserStub();
UserStub.SayHello svcReq = new UserStub.SayHello();
svcReq.setName("my friend.");
UserStub.SayHelloResponse svcRes = stub.sayHello(svcReq);
response.getOutputStream().print(svcRes.get_return());

B.R. :)

Note
1. Axis2 1.6.1 的 eclipse 的 plugin tool 有問題, 請載前一版, 或下一版(1.7)
2. 發佈據說要在環境變數中加入 JAVA_HOME, AXIS2_HOME, JBOSS_HOME
3. 若要再加 Service, 先新增一個class, 並在 .java 檔上點右鍵 => Web Services => Create Web Service