Eclipse Web Tools Platform (WTP) 1.0 提供了強大的 WebService 開發工具鏈,本文通過一個計算長方形面積的實例,詳細演示如何從 WSDL 文檔生成服務端骨架、實現業務邏輯,並自動生成客戶端進行測試。
AreaProj創建 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>
AreaService.wsdl → New → Other → WebServicesrc 目錄下生成 Java 文件,核心實現類為 AreaServiceSOAPImpl.java修改生成的 AreaServiceSOAPImpl.java,填充計算邏輯:
public float calculateRectArea(Dimensions parameters) {
return parameters.getWidth() * parameters.getHeight();
}
WebContent/wsdl/ 下會生成 AreaServiceSOAP.wsdlcalculateRectArea,輸入寬高參數(如 5.0, 3.0)<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>
<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>
web.xml 與 wsdd掌握 WTP 的 WSDL-to-Java 工作流,能顯著提升 WebService 開發效率,特別適合企業級集成場景中契約穩定、頻繁迭代的業務需求。