`

CXF动态客户端No operation was found with the name问题

    博客分类:
  • JAVA
CXF 
阅读更多

如果WebService发布时,接口和实现类指定的namespace不同。

那么使用CXF动态客户端调用时,会抛出No operation was found with the name的异常

 

 

JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = factory.createClient(wsdl);
Object[] res = client.invoke(operation, realParams);

 

用下面的方法临时处理一下吧:

JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = factory.createClient(wsdl);

// 下面一段处理 WebService接口和实现类namespace不同的情况
// CXF动态客户端在处理此问题时,会报No operation was found with the name的异常
Endpoint endpoint = client.getEndpoint();
QName opName = new QName(endpoint.getService().getName().getNamespaceURI(), operation);
BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding();
if (bindingInfo.getOperation(opName) == null) {
	for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) {
		if (operation.equals(operationInfo.getName().getLocalPart())) {
			opName = operationInfo.getName();
			break;
		}
	}
}

Object[] res = client.invoke(opName, realParams);

 或者如果知道确切的namespaceURL

JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance(); 
client = factory.createClient("服务发布地址......?wsdl"); // 记得要加入"?wsdl" 
QName opName = new QName("http://service.com/", "saveLove");  // 指定到接口类所在包 
Object[] res = client.invoke(opName, "猪打天下"); 
System.out.println("Say: " + res[0]); 

 重点:CXF发布用的是业务类(SaveLoveImpl.java),那么默认的命名空间就会是业务类所在包(路径),而对外界暴露的则是接口类(ISaveLove.java),那么对于客户端(第三方)调用访问时,需要按照接口类所在包(路径)进行命名空间的定义,仅此而已。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics