通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了。
![](https://img-blog.csdnimg.cn/img_convert/6eb116e17b0b06a6e4f25ea708bd3c4f.png)
1.静态调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // 创建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.动态调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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类,服务器端只要提供接口名和路径就可以方便的调用。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接