Java调用CXF WebService接口的两种方式实例

2025-05-29 0 87

通过http://localhost:7002/card/services/helloworld?wsdl访问到xml如下,说明接口写对了。

Java调用CXF WebService接口的两种方式实例

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类,服务器端只要提供接口名和路径就可以方便的调用。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/qq_26562641/article/details/71534715

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java调用CXF WebService接口的两种方式实例 https://www.kuaiidc.com/110202.html

相关文章

发表评论
暂无评论