1. 程式人生 > 其它 >CXF 呼叫webservice客戶端報錯:2 counts of InaccessibleWSDLException

CXF 呼叫webservice客戶端報錯:2 counts of InaccessibleWSDLException

背景

專案中介面通訊用到了webservice服務,在具體應用中,呼叫方並非直接訪問服務方,而是通過代理轉發請求以實現訪問。

問題

使用 apache axis1.4 工具生成呼叫服務的客戶端程式碼可以正常訪問;使用apache cxf 工具生成的客戶端程式碼卻報錯:2 counts of InaccessibleWSDLException。

原因

axis 客戶端程式碼是將服務名(service name)、埠名(portType)等資訊固定地寫在生成的程式碼中;而cxf客戶端程式碼是在呼叫介面之前,通過GET獲取到wsdl檔案進行解析,繫結服務名與埠名等資訊。

在專案中,由於使用了代理重定向介面地址,無法通過GET直接獲取到wsdl檔案進行解析,故報錯。

對策

  • a. 將wsdl檔案儲存在本地,客戶端中讀取本地的wsdl檔案,實際介面的地址填寫在wsdl檔案中的soap地址標籤中
<service name="xxxService">
   <port binding="xxxSOAPBinding" name="xxxSOAPPort">
      <soap:address location="http://yourActualAddress/xxxService"/>
   </port>
</service>
  • b. 更改呼叫的程式碼,跳過獲取wsdl的步驟。
xxxService ss = new xxxService(null, SERVICE_NAME);
XXXServicePortType port = ss.getPort(xxxServicePortType.class);

BindingProvider bindingProvider = (BindingProvider) port;
bindingProvider.getRequestContext()
  .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
    "http://www.example.com/real_endpoint_url_goes_here");
Response res = port.yourMethods(...);

參考自:Instantiate JAX-WS service without downloading WSDL?