好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

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

总结

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

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

查看更多关于Java调用CXF WebService接口的两种方式实例的详细内容...

  阅读:33次