生成xfire客户端有多种方式,直接调用xFire暴露的接口、使用xfire eclipse ide生成客户端代码、用ant调用org.codehaus.xfire.gen.WsGenTask声称代码。这里主要讲用ant生成代码的方式。
1、新建一个web项目,导入xFire需要的软件包;
2、在Web-Root下创建build.xml、build.properties,内容分别为:
WebRoot/build.xml
<project name="WebService" basedir="." default="gen-webservice"> <property file="build.properties"> </property> <!--定义类路径--> <path id="project-classpath"> <fileset dir="./WEB-INF/lib"> <!--表示包括lib.dir目录以及子目录的所有的jar文件--> <include name="**/*.jar"> </include> </fileset> </path> <target name="gen-webservice"> <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="project-classpath" /> <wsgen outputDirectory="${src.dir}" wsdl="${wsdl.dir}" package="client" overwrite="true" /> </target> </project>
WebRoot/build.properties
src.dir=${basedir}/src lib.dir=${basedir}/lib wsdl.dir=http\://localhost\:8080/xFire/testService.ws?wsdl
3、运行ant,生成客户端代码。
代码结构如下图
注意红色部分。
4、具体调用代码:
Test.java
package test;
import client.IHelloClient;
import client.IHelloPortType;
public class Test2 {
/**
* @param args
*/
public static void main(String[] args) {
IHelloClient hlo = new IHelloClient();
IHelloPortType helloType = hlo.getIHelloHttpPort();
String rtn = helloType.helloTo("张山");
System.out.println("rtn is " + rtn);
rtn = helloType.hello();
System.out.println("rtn2 is " + rtn);
}
}
运行结果:
rtn is hello 张山!
rtn2 is hello
调用成功!
此种办法不用关心url、接口等,一般情况下,只需要调用指定方法、传递参数就可以调用远程服务。
另一种方法:使用soap UI 工具生成,可以选择支持xfire、axis等。