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

WTP 1.0 實戰:從 WSDL 文檔生成 WebService 服務端與客戶端完整指南

     編輯:Java 相關知識

Eclipse Web Tools Platform (WTP) 1.0 提供了強大的 WebService 開發工具鏈,本文通過一個計算長方形面積的實例,詳細演示如何從 WSDL 文檔生成服務端骨架、實現業務邏輯,並自動生成客戶端進行測試。

環境準備

  • Apache Tomcat 5.0(已安裝並配置)
  • Eclipse WTP 1.0
  • 新建 Dynamic Web Project:AreaProj

步驟 1:定義 WSDL 契約

創建 AreaService.wsdl 並置於 WebContent 目錄,定義計算長方形面積的服務契約:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:soap="https://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:tns="https://tempuri.org/AreaService/"
    xmlns:wsdl="https://schemas.xmlsoap.org/wsdl/"
    xmlns:xsd="https://www.w3.org/2001/XMLSchema"
    name="AreaService"
    targetNamespace="https://tempuri.org/AreaService/">

  <wsdl:types>
    <xsd:schema targetNamespace="https://tempuri.org/AreaService/"
        xmlns:xsd="https://www.w3.org/2001/XMLSchema">
      <xsd:element name="area" type="xsd:float"/>
      <xsd:element name="parameters" type="tns:dimensions"/>
      <xsd:complexType name="dimensions">
        <xsd:sequence>
          <xsd:element name="width" type="xsd:float"/>
          <xsd:element name="height" type="xsd:float"/>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:schema>
  </wsdl:types>

  <wsdl:message name="CalculateRectAreaResponse">
    <wsdl:part element="tns:area" name="area"/>
  </wsdl:message>
  <wsdl:message name="CalculateRectAreaRequest">
    <wsdl:part element="tns:parameters" name="parameters"/>
  </wsdl:message>

  <wsdl:portType name="AreaService">
    <wsdl:operation name="CalculateRectArea">
      <wsdl:input message="tns:CalculateRectAreaRequest"/>
      <wsdl:output message="tns:CalculateRectAreaResponse"/>
    </wsdl:operation>
  </wsdl:portType>

  <wsdl:binding name="AreaServiceSOAP" type="tns:AreaService">
    <soap:binding style="document" transport="https://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="CalculateRectArea">
      <soap:operation soapAction="https://tempuri.org/AreaService/NewOperation"/>
      <wsdl:input><soap:body use="literal"/></wsdl:input>
      <wsdl:output><soap:body use="literal"/></wsdl:output>
    </wsdl:operation>
  </wsdl:binding>

  <wsdl:service name="AreaService">
    <wsdl:port binding="tns:AreaServiceSOAP" name="AreaServiceSOAP">
      <soap:address location="https://tempuri.org"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

步驟 2:生成服務端骨架

  1. 右鍵 AreaService.wsdl → New → Other → WebService
  2. 配置:Service implementation 選擇「Generate from WSDL」,Server runtime 指向 Tomcat 5.0
  3. 一路 Next,若 Tomcat 未啟動會提示啟動,最後 Finish
  4. src 目錄下生成 Java 文件,核心實現類為 AreaServiceSOAPImpl.java

步驟 3:實現業務邏輯

修改生成的 AreaServiceSOAPImpl.java,填充計算邏輯:

public float calculateRectArea(Dimensions parameters) {
    return parameters.getWidth() * parameters.getHeight();
}

步驟 4:生成並測試客戶端

  1. 在 WebContent/wsdl/ 下會生成 AreaServiceSOAP.wsdl
  2. 右鍵該文件 → New → Other → WebService Client
  3. Client environment 配置選擇 Java Proxy,Finish 完成生成
  4. 在測試視圖中選擇 calculateRectArea,輸入寬高參數(如 5.0, 3.0)
  5. 查看測試結果與 SOAP 報文:

Request SOAP

<soapenv:Envelope xmlns:soapenv="https://schemas.xmlsoap.org/soap/envelope/"
    xmlns:tns="https://tempuri.org/AreaService/">
  <soapenv:Body>
    <tns:CalculateRectAreaRequest>
      <tns:parameters>
        <tns:width>5.0</tns:width>
        <tns:height>3.0</tns:height>
      </tns:parameters>
    </tns:CalculateRectAreaRequest>
  </soapenv:Body>
</soapenv:Envelope>

Response SOAP

<soapenv:Envelope xmlns:soapenv="https://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <tns:CalculateRectAreaResponse xmlns:tns="https://tempuri.org/AreaService/">
      <tns:area>15.0</tns:area>
    </tns:CalculateRectAreaResponse>
  </soapenv:Body>
</soapenv:Envelope>

關鍵要點總結

  • 契約優先:WSDL 定義服務接口,雙方獨立開發,降低耦合
  • 工具鏈自動化:WTP 承擔樁代碼生成、序列化/反序列化映射、部署描述符配置
  • SOAP 报文檢查:測試視圖提供原始報文視圖,便於調試協議層問題
  • 部署驗證:Tomcat 啟動後自動發布,無需手動配置 web.xml 與 wsdd

掌握 WTP 的 WSDL-to-Java 工作流,能顯著提升 WebService 開發效率,特別適合企業級集成場景中契約穩定、頻繁迭代的業務需求。

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