首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

使用 Eclipse 與 Java SE 6 建立獨立 Web Service 應用(下)——客戶端開發與 SOAP 監控

     編輯:Java 相關知識

承接上篇(服務端發布),本教程示範如何利用 Eclipse IDE、Java SE 6 內建 wsimport、Ant 腳本生成客戶端代碼、撰寫調用邏輯、命令列部署運行,並以 TCP/IP Monitor 捕獲 SOAP 訊息雙向流向。

建立客戶端專案並生成代碼

  1. Eclipse 新建 Java Project wsClientExample,關聯 Java SE 6 JRE。
  2. 建立套件 com.myfirst.wsClient。
  3. 編寫 build.xml 呼叫 wsimport:
<project default="wsimport">
  <target name="wsimport">
    <exec executable="${java.home}/../bin/wsimport">
      <arg line="-keep -s ./src -p com.myfirst.wsClient -d ./bin
        https://localhost:8080/wsServerExample?wsdl"/>
    </exec>
  </target>
</project>
  1. 先啟動上篇的 RunService(服務端發布 WSDL)。
  2. 右鍵 build.xml → Run As → Ant Build,Console 顯示 BUILD SUCCESSFUL。
  3. 專案重整,com.myfirst.wsClient 下出現 JAX-WS 可攜式工件(Service、Port、請求/回應 Bean 等)。

關鍵區別:wsgen 讀取端點類生成部署工件(服務端);wsimport 讀取 WSDL 生成開發/調用工件(客戶端)。

撰寫與運行客戶端應用

package com.myfirst.wsClient;
import javax.xml.ws.BindingProvider;

public class SayHelloClient {
    public static void main(String[] args) {
        SayHelloService shs = new SayHelloService();
        SayHello sh = (SayHello) shs.getSayHelloPort();
        // 設定端點位址(若 WSDL 中位址非預期)
        ((BindingProvider)sh).getRequestContext().put(
            BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
            "https://localhost:8080/wsServerExample");
        System.out.println(sh.getGreeting("Fiona")); // 輸出: Hello Fiona
    }
}

Eclipse 右鍵 Run As → Java Application 即可見結果。進階版加入互動迴圈:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userName;
while (!(userName = br.readLine()).equalsIgnoreCase("quit")) {
    System.out.println(sh.getGreeting(userName));
}

Ant 整合啟動服務端與客戶端(脫離 IDE)

修改 build.xml 新增 runServer、pause、runClient 目標,spawn="true" 於獨立命令視窗啟動:

<target name="runServer">
  <exec dir="${java.home}/bin/" executable="cmd" spawn="true" os="Windows XP">
    <arg line="start cmd /K java -cp "${basedir}/../wsServerExample/bin" com.myfirst.wsServer.RunService"/>
  </exec>
  <exec executable="xterm" spawn="true" os="Linux"> ... </exec>
</target>
<target name="pause" depends="runServer"><sleep seconds="5"/></target>
<target name="runClient" depends="pause"> ... </target>

執行 Ant Build,雙視窗並行,客戶端持續提示輸入姓名,服務端回應問候,輸入 quit 雙雙退出。

TCP/IP Monitor 捕獲 SOAP 訊息

  1. Window → Show View → Other → Debug → TCP/IP Monitor 開啟視圖。
  2. Preferences → Run/Debug → TCP/IP Monitor → Add 配置:
    • Local monitoring port: 8081
    • Host name: localhost
    • Port: 8080 (服務端實際埠)
    • Type: HTTP
  3. 啟動 Monitor(綠燈)。
  4. 修改客戶端端點位址為 https://localhost:8081/wsServerExample(經 Monitor 轉發)。
  5. 重新運行 Ant 腳本,視圖即顯示請求/回應配對。

SOAP 訊息剖析

請求:

POST /wsServerExample HTTP/1.1
SOAPAction: ""
Content-Type: text/xml; charset=utf-8
...
<S:Envelope xmlns:S="https://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <ns2:getGreeting xmlns:ns2="https://wsServer.myfirst.com/">
      <arg0>Fiona</arg0>
    </ns2:getGreeting>
  </S:Body>
</S:Envelope>

回應:

HTTP/1.1 200 OK
Content-type: text/xml; charset=utf-8
Transfer-encoding: chunked
...
<S:Envelope ...>
  <S:Body>
    <ns2:getGreetingResponse ...>
      <return>Hello Fiona</return>
    </ns2:getGreetingResponse>
  </S:Body>
</S:Envelope>

Envelope → Body → 具體操作元素,清晰對應 Java 方法調用。可選 WS-I 合規性驗證(紅圈圖標)導出日誌離線分析。

附錄:核心術語速查

  • WS-I:Web Services Interoperability 組織,推動跨平台互操作。
  • Envelope:SOAP 訊息根元素,含 Header(控制資訊)與 Body(業務承載)。
  • WSDL:機器可讀的服務契約,描述操作、簽名、綁定、位址。

Java SE 6 + Eclipse 讓獨立 Web Service 開發部署輕量化,無需重型應用伺服器,適合輕量級整合、微服務原型、教學演示等場景。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved