WSDL (Web Services Description Language) 是描述 Web 服務介面的 XML 標準語言。本文以一個完整的 WSDL 範例,逐層解析七大核心元素。
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="HelloService"
targetNamespace="https://example.com/hello"
xmlns:tns="https://example.com/hello"
xmlns:xsd="https://www.w3.org/2001/XMLSchema"
xmlns:soap="https://schemas.xmlsoap.org/wsdl/soap/"
xmlns="https://schemas.xmlsoap.org/wsdl/">
<!-- 1. 類型定義:資料模型 -->
<types>
<xsd:schema targetNamespace="https://example.com/hello">
<xsd:element name="sayHello">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sayHelloResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</types>
<!-- 2. 訊息定義:通信數據單元 -->
<message name="sayHelloRequest">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<!-- 3. 端口類型:抽象操作集合 -->
<portType name="HelloPortType">
<operation name="sayHello">
<input message="tns:sayHelloRequest"/>
<output message="tns:sayHelloResponse"/>
</operation>
</portType>
<!-- 4. 綁定:協議與編碼具體化 -->
<binding name="HelloBinding" type="tns:HelloPortType">
<soap:binding style="document" transport="https://schemas.xmlsoap.org/soap/http"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input><soap:body use="literal"/></input>
<output><soap:body use="literal"/></output>
</operation>
</binding>
<!-- 5. 服務與端點:部署地址 -->
<service name="HelloService">
<port name="HelloPort" binding="tns:HelloBinding">
<soap:address location="https://localhost:8080/hello"/>
</port>
</service>
</definitions>
<part> 組成,對應方法參數/返回值<operation>),每操作含 input/output/fault<soap:address location="..."/>)| 元素 | 關鍵屬性 | 說明 |
|---|---|---|
| definitions | targetNamespace | WSDL 目標命名空間,類似 Java 包名 |
| portType | name | 介面名稱,對應生成代碼的 Service Interface |
| binding | type | 引用 portType 名稱 |
| binding | soap:binding/style | document(文檔式) / rpc(遠程過程調用式) |
| operation | soap:operation/soapAction | HTTP Header 中 SOAPAction 值 |
| port | binding | 引用 binding 名稱 |
| port | soap:address/location | 服務實際訪問 URL |