在XFire中创建WebService客户端的三种常用方式

首先、新建Web Project、并在该项目的根目录下新建build.xml

  1. <?xmlversion="1.0"encoding="UTF-8"?> 
  2. <project name="xfireAnt"basedir="."default="createClientCode"> 
  3.     <propertyname="xfirelib"value="${basedir}/WebRoot/WEB-INF/lib"/> 
  4.     <propertyname="sources"value="${basedir}/src"/> 
  5.     <pathid="classpath"> 
  6.         <filesetdir="${xfirelib}"> 
  7.             <includename="*.jar"/> 
  8.         </fileset> 
  9.     </path> 
  10.          
  11.     <targetname="createClientCode"> 
  12.         <taskdefname="wsgen"classname="org.codehaus.xfire.gen.WsGenTask"classpathref="classpath"/> 
  13.         <wsgenoutputDirectory="${sources}"wsdl="http://www.ayandy.com/Service.asmx?wsdl"package="com.jadyer.client"overwrite="true"/> 
  14.     </target> 
  15. </project> 

然后拷贝以下的Jar包到该项目的//WebRoot//WEB-INF//lib//中

  1. /**
  2. * @see --------------------------------------------------------------
  3. * @see 我们新建的build.xml主要用来生成客户端代码,所以就需要依赖以下的Jar包
  4. * @see 以下的Jar包,除ant-1.8.1.jar外,均取自xfire-distribution-1.2.6.zip
  5. * @see ant-1.8.1.jar
  6. * @see activation-1.1.jar
  7. * @see commons-logging-1.0.4.jar
  8. * @see jaxb-api-2.0.jar
  9. * @see jaxb-impl-2.0.1.jar
  10. * @see jaxb-xjc-2.0.1.jar
  11. * @see jdom-1.0.jar
  12. * @see stax-api-1.0.1.jar
  13. * @see wsdl4j-1.6.1.jar
  14. * @see wstx-asl-3.2.0.jar
  15. * @see xbean-2.2.0.jar
  16. * @see xfire-all-1.2.6.jar
  17. * @see xfire-jsr181-api-1.0-M1.jar
  18. * @see XmlSchema-1.1.jar
  19. * @see --------------------------------------------------------------
  20. * @see 我们自己所要编写的客户端调用类,在执行时,需要用到以下的两个Jar包
  21. * @see 这两个Jar包,同样取自xfire-distribution-1.2.6.zip
  22. * @see commons-codec-1.3.jar
  23. * @see commons-httpclient-3.0.jar
  24. * @see --------------------------------------------------------------
  25. */ 

然后我们执行build.xml文件,即Run As——Ant Build

待控制台提示BUILD SUCCESSFUL时,刷新当前Web项目

就会发现,Ant已经成功帮我们生成了客户端代码

接下来,我们再编写一个类,调用客户端代码,实现天气查询功能

  1. package com.jadyer.test; 
  2. import java.util.List; 
  3. import org.tempuri.ArrayOfString; 
  4. import org.tempuri.TheDayFlagEnum; 
  5. import com.jadyer.client.ServiceClient; 
  6. /**
  7. * 提供免费天气服务的网站: http://www.ayandy.com
  8. */ 
  9. public class ClientFromAnt { 
  10.     public staticvoid main(String[] args) { 
  11.         //输出程序执行的开始时间 
  12.         long startTime = System.currentTimeMillis(); 
  13.         System.out.println("开始时间: " + startTime); 
  14.          
  15.         //调用天气服务的核心代码 
  16.         ServiceClient client = new ServiceClient(); 
  17.         ArrayOfString weather = client.getServiceSoap().getWeatherbyCityName("哈尔滨", TheDayFlagEnum.TODAY); 
  18.         List<String> weatherList = weather.getString(); 
  19.         for(Object obj:weatherList.toArray()){ 
  20.             System.out.println(obj); 
  21.         } 
  22.          
  23.         //输出程序的执行时间 
  24.         long endTime = System.currentTimeMillis(); 
  25.         System.out.println("结束时间: " + endTime); 
  26.         System.out.println("耗费时间: " + (endTime - startTime)); 
  27.     } 

最后控制台打印类似下面的输出,即实现了天气预报功能

  1. 开始时间: 1294654106437 
  2. 2011-1-10 18:08:30 org.apache.commons.httpclient.HttpMethodBase writeRequest 
  3. 信息: 100 (continue) read timeout. Resume sending the request 
  4. null 
  5. 哈尔滨 
  6. 晴 
  7. -16 ~ -25 ℃ 
  8. 微风 
  9. 今天 
  10. http://www.ayandy.com/images/晴.gif 
  11. null 
  12. 结束时间: 1294654111093 
  13. 耗费时间: 4656 

转载地址:http://blog.csdn.net/jadyer/article/details/6127517

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值