調用cxf 有3中方式:如下
1,生成webservice https://www.cnblogs.com/moon521/p/5564504.html
2.靜態調用
// 創建WebService客戶端代理工廠
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
// 判斷是否拋出異常
factory.getOutInterceptors().add(new LoggingInInterceptor());
// 注冊webservice接口
factory.setServiceClass(DeductionService.class);
// 配置webservice地址
factory.setAddress("http://localhost:7002/card/services/HelloWorld?wsdl");
// 獲得接口對象
CxfService service = (CxfService) factory.create();
// 調用接口方法
String result = service.sayHello("aaaaaaaaaa");
System.out.println("調用結果:" + result);
// 關閉接口連接
System.exit(0);
3.動態調用:
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf
.createClient("http://localhost:7002/card/services/HelloWorld?wsdl");
// url為調用webService的wsdl地址
QName name = new QName("http://dao.xcf.digitalchina.com/", "sayHello");
// namespace是命名空間,methodName是方法名
String xmlStr = "aaaaaaaa";
// paramvalue為參數值
Object[] objects;
try {
objects = client.invoke(name, xmlStr);
System.out.println(objects[0].toString());
} catch (Exception e) {
e.printStackTrace();
}
區別:靜態調用需要依賴service類,因為客戶端調用cxf webservice接口的過程中需要服務器端提供service,很不方便,如果同一個項目中則沒有區別。
動態調用完全不依賴service類,服務器端只要提供接口名和路徑就可以方便的調用。