WSDL 中的「绑定(binding)」到底是干什么的?彻底讲明白(2025 年最实用版)
一句话总结:
binding 就是“把抽象接口(portType)翻译成真实网络协议的规则书”,它回答了“到底用 SOAP?用 HTTP GET?用什么格式包装数据?”这些问题。
WSDL 层级关系(记住这张图就再也不迷糊)
portType(抽象接口)
↓ binding 负责翻译
binding(具体协议规则)
↓ port 负责填地址
port / endpoint(真实 URL)
binding 里到底写了哪几件事?(5 个核心点)
| 写什么 | 举例说明 | 实际意义 |
|---|---|---|
| 1. 用什么底层协议 | SOAP over HTTP、HTTP GET/POST、JMS、SMTP… | 99.9% 都是 SOAP over HTTP |
| 2. SOAP 版本 | SOAP 1.1 还是 SOAP 1.2 | 决定你 envelope 命名空间和 Content-Type |
| 3. style(编码风格) | document(推荐) 或 rpc(老古董) | 决定 XML 长得“干净”还是“恶心” |
| 4. use(序列化方式) | literal(推荐) 或 encoded(几乎被淘汰) | literal = 按 XSD 直接生成,encoded = 带类型信息 |
| 5. 每个 operation 的细节 | soapAction、输入输出怎么包 | 客户端必须严格遵守 |
2025 年真正推荐的 4 种 binding 写法(直接抄)
<!-- 1. 最推荐:SOAP 1.1 + document/literal wrapped(99% 项目都用这个) -->
<binding name="UserSoap11Binding" type="tns:UserPortType">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetUser">
<soap:operation soapAction="http://example.com/GetUser"/>
<input><soap:body use="literal"/></input>
<output><soap:body use="literal"/></output>
</operation>
</binding>
<!-- 2. SOAP 1.2 版本(大厂新项目越来越常见) -->
<binding name="UserSoap12Binding" type="tns:UserPortType">
<soap12:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetUser">
<soap12:operation soapAction="http://example.com/GetUser"/>
<input><soap12:body use="literal"/></input>
<output><soap12:body use="literal"/></output>
</operation>
</binding>
<!-- 3. 老系统常见:rpc/encoded(看到就想跑) -->
<binding name="OldRpcBinding" type="tns:UserPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetUser">
<soap:operation soapAction=""/>
<input><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/></input>
<output><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/></output>
</operation>
</binding>
<!-- 4. 纯 HTTP 绑定(不走 SOAP,极少用) -->
<binding name="UserHttpBinding" type="tns:UserPortType">
<http:binding verb="POST"/>
<operation name="GetUser">
<http:operation location="/GetUser"/>
<input><mime:content type="application/xml"/></input>
<output><mime:content type="application/xml"/></output>
</operation>
</binding>
不同 binding 生成的 SOAP 包对比(一眼看出差别)
| binding 类型 | 请求包里 里长这样 | 客户端代码是否好看 |
|---|---|---|
| document/literal wrapped | 123 | 非常干净(推荐) |
| document/literal(非wrapped) | 123 | 还行 |
| rpc/encoded | 123 + 一堆 xsi:type | 恶心死了 |
| rpc/literal | 123 | 一般 |
实际项目里怎么选?
| 项目类型 | 必须选的 binding |
|---|---|
| 银行、运营商、税务 | SOAP 1.1 document/literal wrapped |
| 新建微服务(2024~2025) | SOAP 1.2 document/literal wrapped |
| 老系统对接(10年前的) | 可能被迫用 rpc/encoded |
| 想完全抛弃 SOAP | 不用 WSDL,改 REST + OpenAPI |
总结口诀(背下来永不迷糊)
“portType 定方法,binding 定玩法,port 定地址”
binding 就是“翻译官”,它决定你最终看到的 SOAP 包是美还是丑。
需要我给你:
- 带 WS-Security、MTOM 附件的进阶 binding 示例
- Spring Boot + CXF 自动生成完美 binding 的配置
- 或者教你怎么用 Wireshark 抓包判断对方到底用了哪种 binding
直接说一声!