首页 > 技术文章 > CXF 调用webservice客户端报错:2 counts of InaccessibleWSDLException

kafkasdream 2022-03-04 11:56 原文

背景

项目中接口通信用到了webservice服务,在具体应用中,调用方并非直接访问服务方,而是通过代理转发请求以实现访问。

问题

使用 apache axis1.4 工具生成调用服务的客户端代码可以正常访问;使用apache cxf 工具生成的客户端代码却报错:2 counts of InaccessibleWSDLException。

原因

axis 客户端代码是将服务名(service name)、端口名(portType)等信息固定地写在生成的代码中;而cxf客户端代码是在调用接口之前,通过GET获取到wsdl文件进行解析,绑定服务名与端口名等信息。
image
在项目中,由于使用了代理重定向接口地址,无法通过GET直接获取到wsdl文件进行解析,故报错。

对策

将wsdl文件存储在本地,客户端读取本地的wsdl文件。
在客户端xxxService.java中的注解@WebServiceClient()中重新指定wsdlLocation指向classpath中的wsdl文件。
若要变更接口服务的地址,有两种方式:

  • a. 将地址写在wsdl文件中的soap地址标签中
<service name="xxxService">
   <port binding="xxxSOAPBinding" name="xxxSOAPPort">
      <soap:address location="http://yourActualAddress/xxxService"/>
   </port>
</service>
  • b. 更改调用的代码,重新指定接口服务的地址。
xxxService ss = new xxxService(null, SERVICE_NAME);

XXXServicePortType port = ss.getxxxSOAPPort();
// 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?

推荐阅读