Web Service 是異構系統集成的基石。掌握 WSDL、SOAP、SEI 等核心術語的本質與協作關係,是設計、調試、治理企業級服務的前提。本文結合協議規範與 Apache CXF 實戰,構建完整知識體系。
WSDL (Web Services Description Language) 是基於 XML 的接口描述語言,定義服務提供什麼操作、輸入輸出格式、通訊協議、網絡地址。一個服務對應一個 WSDL 文檔(或模組化導入多個)。
<definitions ...>
<types> <!-- 數據類型定義 (XML Schema) -->
<schema>...</schema>
</types>
<message name="..."> <!-- 抽象消息:由 part 組成 -->
<part name="..." type="..."/>
</message>
<portType name="..."> <!-- 抽象接口:操作集合 -->
<operation name="...">
<input message="..."/>
<output message="..."/>
<fault message="..."/>
</operation>
</portType>
<binding name="..." type="..."> <!-- 具體協議綁定 (SOAP/HTTP) -->
<soap:binding style="document" transport="https://schemas.xmlsoap.org/soap/http"/>
<operation>...</operation>
</binding>
<service name="..."> <!-- 服務端點集合 -->
<port name="..." binding="...">
<soap:address location="https://host:port/context/endpoint"/>
</port>
</service>
</definitions>
portType 定義「做什麼」,binding 定義「怎麼做」。wsimport (JDK 內置)、wsdl2java (CXF)、SoapUI 測試。SOAP (Simple Object Access Protocol) 是基於 XML 的輕量級消息交換協議,運行於 HTTP/HTTPS/SMTP 等傳輸層之上。核心特點:擴展性、中立性、獨立性。
<soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
<soap:Header> <!-- 可選:認證、事務、路由等擴展信息 -->
<wsse:Security>...</wsse:Security>
</soap:Header>
<soap:Body> <!-- 必選:業務負載 -->
<ns:getUserResponse>
<user><id>1</id><name>Alice</name></user>
</ns:getUserResponse>
</soap:Body>
<soap:Fault> <!-- 錯誤時出現 -->
<faultcode>soap:Server</faultcode>
<faultstring>Database unavailable</faultstring>
</soap:Fault>
</soap:Envelope>
| 風格 | 特點 | 適用場景 |
|---|---|---|
| Document/Literal | Body 為完整 XML 文檔片段,語義豐富,支持驗證 | 企業級集成、文檔式交互 (推薦) |
| RPC/Literal | Body 模擬遠程調用結構,參數順序固定 | 傳統 RPC 遷移、簡單場景 |
application/soap+xml MIME 類型、更豐富的 Fault 代碼、XML Infoset 綁定。bindingId 指定。SEI (Service Endpoint Interface) 是標註 @WebService 的 Java 接口,直接映射 WSDL portType。它是服務端實現類與客戶端代理的共享契約。
@WebService(name = "UserService", targetNamespace = "https://example.com/user")
@SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL, parameterStyle = ParameterStyle.WRAPPED)
public interface UserService {
@WebMethod(operationName = "getUser")
@WebResult(name = "user")
User getUser(@WebParam(name = "id") Long id);
@WebMethod
List<User> listUsers(@WebParam(name = "criteria") QueryCriteria criteria);
@WebMethod(exclude = true) // 不暴露為操作
void internalInit();
}
@WebService:聲明為 SEI,指定名稱、命名空間、wsdlLocation。@SOAPBinding:控制風格、編碼、參數包裝方式。@WebMethod:暴露為操作,可自定義 operationName、action。@WebParam/@WebResult:映射消息 part 名稱、命名空間、header 標誌。@OneWay:單向調用,無響應 (HTTP 202)。CXF = Celtix (ObjectWeb ESB 核心) + XFire (Web Service 引擎)。支持 JAX-WS 2.x / JAX-RS 2.x、REST、SOAP、CORBA、JSON、gRPC 等多協議,可獨立部署或嵌入 Spring Boot/OSGi/Servlet 容器。
# pom.xml
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.6.0</version>
</dependency>
# application.yml
cxf:
path: /services # 服務發布根路徑
servlet:
init:
# 可選:發布 WSDL 位置、啟用 MTOM 等
# 服務發布配置類
@Configuration
public class CxfConfig {
@Autowired
private UserServiceImpl userService; // 實現類
@Bean
public Endpoint userEndpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), userService);
endpoint.publish("/UserService"); // 完整路徑 /services/UserService
// 可選:啟用 WSDL 獲取、Schema 驗證、Logging 攔截器
endpoint.getInInterceptors().add(new LoggingInInterceptor());
endpoint.getOutInterceptors().add(new LoggingOutInterceptor());
return endpoint;
}
}
// 1. 動態代理 (JAX-WS 標準)
@Bean
public UserService userClient() {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(UserService.class);
factory.setAddress("https://remote-host:8080/services/UserService");
return (UserService) factory.create();
}
// 2. WebServiceTemplate (Spring WS 風格,需 marshaller)
// 適用於複雜 XML 構建/解析場景
[Java Interface (SEI)]
│ @WebService 註解
▼
[JAX-WS Runtime (CXF)] ──動態生成──▶ [WSDL 文檔] ◀── 客戶端導入 (wsimport/wsdl2java)
│ │
│ SOAP Binding 配置 │ 定義操作、消息、綁定
▼ ▼
[SOAP Message (XML)] ◀───HTTP/HTTPS───▶ [SOAP Message (XML)]
│ │
▼ ▼
[服務端實現類] [客戶端代理/Stub]
│ │
└────────────── 業務邏輯執行 ──────────┘
WSDL 定義契約,SOAP 承載數據,SEI 映射代碼,CXF 驅動運行。理解這四者的分層職責與協作流程,能讓你在服務治理、版本演進、跨語言互操作、安全加固等場景遊刃有餘。微服務時代雖 REST/gRPC 盛行,但 SOAP/Web Service 在金融、電信、政府等強契約、強安全、長鏈路事務領域仍不可替代。