CXF学习笔记(2)-HelloWorld!-客户端调用

上一节中已经成功的发布了一个服务端webservice,这一部分将默认客户端调用webservice

这里我们重新建立了一个新的工程CXF-Client,把上一节中列举的jar包都搞过来即可,调用方式采用了和RMI类似的机制,即客户端直接服务器端提供的服务接口(interface),CXF通过运行时代理生成远程服务的代理对象,在客户端完成对webservice的访问

几个必填的字段:setAddress-这个就是我们发布webservice时候的地址,保持一致

HelloWorldService:需要服务器端提供单独的Jar文件过来

package com.crazycoder2010.webservice.cxf.client;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.crazycoder2010.webservice.cxf.server.HelloWorldService;

public class Client {
	public static void main(String[] args) {
		JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();
		bean.getInInterceptors().add(new LoggingInInterceptor());
		bean.getInFaultInterceptors().add(new LoggingOutInterceptor());
		bean.setServiceClass(HelloWorldService.class);
		bean.setAddress("http://localhost:9090/helloWorldService");
		HelloWorldService helloWorldService = (HelloWorldService)bean.create();
		String result = helloWorldService.sayHello("Kevin");
		System.out.println(result);
	}
}

运行输出:

2011-8-9 21:32:20 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://server.cxf.webservice.crazycoder2010.com/}HelloWorldServiceService from class com.crazycoder2010.webservice.cxf.server.HelloWorldService
2011-8-9 21:32:41 org.apache.cxf.interceptor.AbstractLoggingInterceptor log
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset="utf-8"
Headers: {Content-type=[text/xml;charset="utf-8"], Transfer-encoding=[chunked]}
Payload: <?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayHelloResponse xmlns:ns2="http://server.cxf.webservice.crazycoder2010.com/"><return>Hello Kevin</return></ns2:sayHelloResponse></S:Body></S:Envelope>
--------------------------------------
Hello Kevin

小结:这种调用service的好处在于调用过程非常简单,就几行代码就完成一个webservice的调用,但是客户端也必须依赖服务器端的接口,这种调用方式限制是很大的,要求服务器端的webservice必须是java实现--这样也就失去了使用webservice的意义

查看了官方的文档后,发现其实是可以实现不依赖服务器端接口来完成调用的,如下

使用JaxWsDynamicClientFactory类,只要指定服务器端wsdl文件的位置,然后指定要调用的方法和方法的参数即可,so simple!!

package com.crazycoder2010.webservice.cxf.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class Client3 {
	public static void main(String[] args) throws Exception {
		JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
		Client client = clientFactory.createClient("http://localhost:9090/helloWorldService?wsdl");
		Object[] result = client.invoke("sayHello", "KEVIN");
		System.out.println(result[0]);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值