使用 CXF 自帶 wsdl2java 工具基於 HTTPS WSDL 生成客戶端代碼時,因信任庫缺少服務端證書,報錯:
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target
不想/無法導入證書時,可用 SoapUI 圖形化工具離線生成代碼,繞過 SSL 驗證。
菜單 File → Preferences → SSL Settings:
菜單 File → Preferences → CXF Settings:
bin/wsdl2java、lib 等)。https://example.com/service?wsdl)。com.example.client)。com.example.client
├── ServiceName.java // Service 接口
├── ServiceName_Service.java // Service 工廠類(含 getPort())
├── PortName.java // SEI 接口(業務方法)
├── ObjectFactory.java // JAXB 元素工廠
├── package-info.java // JAXB 命名空間
└── model/ // 複雜類型對象
ServiceName_Service factory = new ServiceName_Service();
ServiceName port = factory.getServiceNamePort();
// 若運行時仍需跳過 SSL(生產請配置正規證書)
BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://real-endpoint.com/service");
bp.getRequestContext().put("com.sun.xml.ws.transport.https.client.SSLSocketFactory", createTrustAllSSLSocketFactory());
| 場景 | 建議 |
|---|---|
| 生產環境 | 務必配置正規證書(導入 TrustStore 或使用 CA 簽發證書),禁用驅動僅限開發/測試。 |
| WSDL 變更 | 重新生成代碼,或手動修改 JAXB 註解適配。 |
| Maven 集成 | 推薦用 cxf-codegen-plugin 在構建階段自動生成,配置 wsdlOptions 指定 HTTPS URL 並設置 trustAll。 |
| 代碼維護 | 生成代碼納入版本控制,避免重複生成衝突。 |
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.4.x</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>https://example.com/service?wsdl</wsdl>
<extraargs>
<extraarg>-client</extraarg>
<extraarg>-trustAll</extraarg> <!-- 關鍵:信任所有證書 -->
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals><goal>wsdl2java</goal></goals>
</execution>
</executions>
</plugin>
掌握 SoapUI 圖形化生成與 Maven 插件自動化生成兩種方式,可靈活應對開發調試與持續集成場景,快速解決 HTTPS WSDL 代碼生成阻塞問題。