通過http://localhost:7002/card/services/HelloWorld?wsdl訪問到xml如下,說明接口寫對了。
1.靜態調用
// 創建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);
2.動態調用:
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類,服務器端只要提供接口名和路徑就可以方便的調用。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VeVb武林網的支持。
新聞熱點
疑難解答
圖片精選