环境:cxf webservice 动态访问
项目中遇到问题,后台原来用的namespace是实现方法的包名,造成一般的动态访问方式如下:
JaxWsDynamicClientFactory dcf =JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient("http://10.71.253.165:8080/TdtSHCXF/service/cgcxf?wsdl");
Object[] objects = null;
try {
objects = client.invoke("AddWxEvent", new Object[] {jsonString});
//System.out.println(objects[0].toString());
resp.getWriter().write(objects[0].toString());
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
CXF动态客户端在处理此问题时,会报No operation was found with the name的异常,所以应该用下面的代码或者把namspace变成接口的包名来解决:
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = factory.createClient(url);
//处理 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[] objects = null;
try {
objects = client.invoke(opName, new Object[] {jsonString});
resp.getWriter().write(objects[0].toString());
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}