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> 
 

你可能感兴趣的:(java,工作)