Java调用Web服务(Web Services),如此简单

<br>实在不才啊,今天才发现jdk6里有个wsimport.exe,使用可以生成目标web服务器的本地调用类,然后就可以在本地程序中像使用本地代码一样调用webservice了,如此一来和dotnet下的web服务的使用方法就基本一样。(活到老学到老啊<img src="/images/smiles/icon_cry.gif" alt="">)
使用方法如下:
<span style="color: #0000ff;">1、执行wsimport命令:</span>
wsimport http://localhost:9999/accountservice?wsdl
<span style="color: #0000ff;"> 2、将生成的代码进行打包:</span>
jar cvf com.zywang.cxf.jar com/zywang/cxf/
 <span style="color: #0000ff;">3、将生成的jar包添加到项目的构建路径中,进行引用</span>,从使用的情况看java的web服务和dotnet的稍有不同
 
我分别使用dotnet、spring和cxf创建了三个相同功能的web服务,以下是客户端代码片段
<span style="color: #ff0000;">访问dotnet的web服务:</span>
/** * @author zywang 2011-4-19 */public static void main(string[] args) {	accountservice service = new accountservice();	accountservicesoap servicesoap = service.getaccountservicesoap();	account account = new account();	account.setname("hello zywang " + (new date()));	servicesoap.insertaccount(account);	arrayofaccount arrayofaccount = servicesoap.getaccounts();	list<account> accounts = arrayofaccount.getaccount();	for (account a : accounts) {		system.out.println(a.getname());	}}
<span style="color: #ff0000;">访问spring的web服务:</span>
/** * @author zywang 2011-4-19 */public static void main(string[] args) {	accountservice_service service = new accountservice_service();	accountservice accountservice = service.getaccountserviceport();	account account = new account();	account.setname("王朝阳 by ws " + new date());	accountservice.insertaccount(account);	list<account> accounts = accountservice.getaccounts("");	for (account account2 : accounts) {		system.out.println(account2.getname());	}}
<span style="color: #ff0000;">访问cxf的web服务:</span>
/** * @author zywang 2011-4-19 */public static void main(string[] args) {	accountdao_service service = new accountdao_service();	accountdao accountservice = service.getaccountdaoport();	account account = new account();	account.setname("hello 王朝阳 by cxf " + (new date()));	accountservice.insertaccount(account);	list<account> list = accountservice.getaccounts("");	for (account a : list) {		system.out.println(a.getname());	}}
 
因为主要是使用wsimport和jar这两个命令,所以写了个小工具来自动生成jar包,在附件中(源码可以反编译得到)
附件中还有三种web服务的程序,供需要的朋友参考。
工具运行截图:<br><br><img src="http://dl.iteye.com/upload/attachment/467125/761d05c7-2558-31e8-8b6f-ed6bc495ea75.jpg" alt=""><br> <br> 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值