- 簡單的小小範例練習
- 製作web service 有兩種路徑 , top down 與 bottom up
- Bottom Up 是由 Object Class 和 Functional Class 先做起,做起後放上 Application Server 由 Application Server自動產生出 XML Schema (XSD) 和 Web Services Description Language (WSDL)。 方法簡單 , 所以適合初學者練習 , 就算不懂得 XML Schema 和 Web Services Description Language 也一樣可以製作出一個 Web Service。
- Top Down 則反之
- 環境準備
- jboss studio 9.1 ( eclipse 也可)
- java jdk 1.8.0_92
- Apache CXF2 (幫助IDE產生WS的框架)
- tomcat 8.0
- 環境設定
- CXF 2設定 : Windows-->Preferences-->Web Service -->CXF 2.x Preferences
-
將tomcat 加入server

- 因為我的建立方式為bottom up , 所以需要建立class與 function
1. 建立 Server 端
- 先新建一個 war project WS_Server , 並建立一個class EchoServer

- 建立方法 echo

- 在EchoServer 類別上按右鍵選擇Web Service->Create Web Service

- 選擇 bottom up 、 tomcat 、CXF 、War Project

- 然後不斷下一步至 start server 後 , 可以開啟以下網址 , 如有資料代表成功
http://localhost:8080/WS_Server/services/EchoServerPort?wsdl

- CXF套件會自動幫我們產出Web Service的相關檔案
2. 建立 Client 端
- 新建一個 Java Project WS_Client
- 在專案的WS_Client的 src 右鍵 --> other --> Web Service Client

- 輸入相關資訊 , 將server 端的 url 填入 , 注意server需在運行中才找的到

- 按下確認後 , 套件會為我們自動產生package server與 Web Service Client的相關類別 。 手動新增一個package client 與 含main的EchoDemo類別

- 在main 填入以下內容 , 呼叫 service端的echo方法

- 執行WS_Client , 查看console是否得到回覆即成功