XFire的Web Servicep客户端快速配置

一、把XFire及其需要的包都拷过来

下载的XFire包,及其解压后,lib目录下全部的包都拷过来

在项目的根目录下建立一个ant文件(BUILD.XML),必须指出lib目录所在的路逗头衿鞫说膚sdl路径:

<projectname="struts-hibernate-eg"basedir="."default="help">

<pathid="build.classpath">

<filesetfile="lib/*.jar"/>

</path>

<!--Help-->

<targetname="help">

<echomessage="BASEDIR:${basedir}"/>

<echomessage="targer:help"/>

<echomessage="usage:ant"/>

</target>

<!--Wsgen-->

<targetname="Wsgen">

<taskdefname="wsgen"classname="org.codehaus.xfire.gen.WsGenTask"classpathref="build.classpath"/>

<wsgenoutputDirectory="bin"wsdl="http://192.168.0.16:8080/MathService/services/MathService?wsdl"package="client"/>

</target>

</project>

二、运行通过ant运行这个文件,即通过服务器的wsdl类热核,生成客户端所需要的类

其中MathServiceClient.java和MathServicePortType.java,这两个类是最重要的,其他的可要可不要

三、编写自己的类,调用客户端类,即可以远程访问WebService,并调用其对应的类

publicclassTestClient{

publicstaticvoidmain(String[]args){

MathServiceClientmsc=newMathServiceClient();

MathServicePortTypemsp=msc.getMathServiceHttpPort();

System.out.println(msp.add(100,200));

System.out.println(msp.sub(100,200));

}

}

更复杂的生成webservice客户端调用文件的例子:

xfire提供了两种生成客户端测试的方式,一种提供了ant脚本,另一种是提供了xfire的eclipse插件;本文介绍了使用ant脚本的方式生成客户端的方式。

首先在项目XFireProject中增加一个build.xml文件。xfire提供了一个ant任务:

<taskdefname="wsgen"classname="org.codehaus.xfire.gen.WsGenTask"classpathref="myclasspath"/>

build.xml文件的内容如下:

<?xmlversion="1.0"?>

<projectname="XFireProject"default="genfiles"basedir=".">

<propertyname="lib"value="lib"/>

<pathid="myclasspath">

<filesetdir="${lib}">

<includename="*.jar"/>

</fileset>

<pathelementlocation="${genfiles}"/>

</path>

<!--通过XFireant任务生成客户端代码的存放位置-->

<propertyname="code_path"value="src.client"/>

<!--需要生成客户端代码的wsdl文件-->

<propertyname="wsdl_path"value="http://localhost:8080/xfire/services/HelloService?wsdl"/>

<!--生成客户端代码的包名-->

<propertyname="code_package"value="com.liuxiang.xfire.client"/>

<!--Removeclassesdirectoryforcleanbuild-->

<targetname="clean"description="Prepareforcleanbuild">

<deletedir="${code_path}"/>

<mkdirdir="${code_path}"/>

</target>

<!--<targetname="genfiles"depends="clean"description="Generatethefiles">-->

<targetname="genfiles"description="Generatethefiles">

<taskdefname="wsgen"classname="org.codehaus.xfire.gen.WsGenTask"classpathref="myclasspath"/>

<!--outputDirectory属性定义创建的代码所在的文件夹

wsdl是web服务的wsdl文件

package代表创建的代码的package

-->

<wsgenoutputDirectory="${code_path}"wsdl="${wsdl_path}"package="${code_package}"binding="xmlbeans"/>

</target>

</project>

执行ant脚本,将会生成客户端代码,共三个文件。会放在包com.liuxiang.xfire.client下面,文件分别是:

HelloServiceClient.java、HelloServiceImpl.java、HelloServicePortType.java

编写测试代码,通过调用5中生成的代码,编写TestClient.java文件。文件内容如下:

/***//**

*

*/

packagecom.liuxiang.xfire;

importjava.net.MalformedURLException;

importorg.codehaus.xfire.XFire;

importorg.codehaus.xfire.XFireFactory;

importorg.codehaus.xfire.client.XFireProxyFactory;

importorg.codehaus.xfire.service.Service;

importorg.codehaus.xfire.service.binding.ObjectServiceFactory;

importcom.liuxiang.xfire.client.HelloServiceClient;

importcom.liuxiang.xfire.client.HelloServicePortType;

/***//**

*通过XFire生成的客户端进行调用

*

*TestClient.java

*com.liuxiang.xfire

*XFireProject

*@authorliuxiangmailto:juxtapose@163.com

*2007-9-9下午06:54:36

*

*/

publicclassTestClient...{

/***//**

*客户端测试

*通过ant脚本生成的客户端进行调用

*

*@paramname传入的参数,客户名字

*@return返回sayHello()的返回值

*/

publicstaticStringtestClient(Stringname)...{

HelloServiceClienthelloSC=newHelloServiceClient();

HelloServicePortTypehelloSP=helloSC.getHelloServiceHttpPort();

Stringresult=helloSP.sayHello(name);

returnresult;

}

/***//**

*@paramargs

*@throwsException

*/

publicstaticvoidmain(String[]args)throwsException...{

System.out.println(testClient("Liuxiang"));

}

}

相关推荐