WSDL (Web Services Description Language) 是描述 Web 服務接口的 XML 語言,包含兩大部分:
<types>、<message>、<portType> — 平台無關,定義「做什麼」。<binding>、<service>、<port> — 綁定具體協議(SOAP/HTTP)、地址,定義「怎麼做」。@WebService
public interface GreetingService {
String sayHello(String name);
}
@WebService(endpointInterface = "com.demo.GreetingService")
public class GreetingServiceImpl implements GreetingService {
public String sayHello(String name) { return "Hello, " + name; }
}
Bottom up Java bean Web Service,運行時選 Apache Axis2 或 CXF。WEB-INF/wsdl/GreetingService.wsdl、services.xml 等。.wsdl 文件放入項目。| 運行時 | 特點 | 適用場景 |
|---|---|---|
| Apache Axis2 | 成熟、功能全、配置複雜 | 企業級舊系統維護 |
| Apache CXF | 輕量、Spring 集成好、JAX-RS/JAX-WS 雙支持 | 現代 Spring Boot 項目首選 |
| Metro (JAX-WS RI) | 標準實現、GlassFish 內置 | Java EE 兼容性要求高 |
Eclipse 向導屬於舊世代工具鏈,現代項目建議:
wsimport (JDK 內置) 或 cxf-codegen-plugin (Maven) 從 WSDL 生成代碼。spring-boot-starter-webservices (CXF) 或 spring-boot-starter-web (REST),用 @WebService 註解暴露 SOAP,或直接寫 REST API。@EnableWs + Wsdl11Definition Bean,無需手動維護 XML。# 命令行生成客戶端代碼測試
wsimport -keep -p com.demo.client https://localhost:8080/ws/greeting?wsdl
掌握 WSDL 兩層結構與雙向生成流程,無論維護舊 SOA 還是設計新微服務契約,都能游刃有餘。