加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 00:25:55 所属栏目:安全 来源:网络整理
导读:首先、新建Web Project、并在该项目的根目录下新建build.xml [xhtml] view plain copy print ? ? xml version = "1.0" encoding = "UTF-8" ? ? project name = "xfireAnt" basedir = "." default = "createClientCode" ? ??? property name = "xfirelib" val

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

[xhtml] view plain copy print ?
  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//中

[java] view plain copy print ?
  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已经成功帮我们生成了客户端代码

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

[java] view plain copy print ?
  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. ??? }?
  28. }?

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

[ruby] view plain copy print ?
  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://www.voidcn.com/article/p-qdbdlhni-baw.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读