cxf调用接口的几种方式_Java调用CXF WebService接口的两种方式实例

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

总结

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache CXF是一个开源的WebService框架,可以帮助用户快速、简便地开发和部署WebService应用程序。它提供了多种方式调用WebService接口,下面介绍几种常用的方式: 1. 使用JAX-WS API:CXF实现了JAX-WS API,可以直接使用JAX-WS提供的API来调用WebService。示例代码如下: ```java HelloWorldService service = new HelloWorldService(); HelloWorld port = service.getHelloWorldPort(); String result = port.sayHello("CXF"); ``` 2. 使用代理方式CXF可以根据WebService WSDL文件自动生成代理类,然后通过调用代理类的方法来调用WebService接口。示例代码如下: ```java JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(HelloWorld.class); factory.setAddress("http://localhost:8080/HelloWorld"); HelloWorld client = (HelloWorld) factory.create(); String result = client.sayHello("CXF"); ``` 3. 使用Spring配置文件:CXF提供了Spring配置文件方式来实现WebService接口调用。用户可以在Spring配置文件中配置WebService客户端,然后通过Spring容器来获取WebService客户端实例。示例代码如下: ```xml <jaxws:client id="helloClient" serviceClass="com.example.HelloWorld" address="http://localhost:8080/HelloWorld"/> ``` ```java ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); HelloWorld client = (HelloWorld) context.getBean("helloClient"); String result = client.sayHello("CXF"); ``` 以上是几种常用的调用WebService接口方式,可以根据具体情况选择适合自己的方式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值