環(huán)境:cxf ?webservice 動(dòng)態(tài)訪問(wèn)
項(xiàng)目中遇到問(wèn)題古毛,后臺(tái)原來(lái)用的namespace是實(shí)現(xiàn)方法的包名,造成一般的動(dòng)態(tài)訪問(wèn)方式如下:
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 自動(dòng)生成的 catch 塊
e.printStackTrace();
}
CXF動(dòng)態(tài)客戶端在處理此問(wèn)題時(shí)檐盟,會(huì)報(bào)No operation was found with the name的異常褂萧,所以應(yīng)該用下面的代碼或者把namspace變成接口的包名來(lái)解決:
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = factory.createClient(url);
//處理 WebService接口和實(shí)現(xiàn)類(lèi)namespace不同的情況
// CXF動(dòng)態(tài)客戶端在處理此問(wèn)題時(shí),會(huì)報(bào)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 自動(dòng)生成的 catch 塊
e.printStackTrace();
}