XFire的Web Servicep客户端快速配置

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

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

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

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

<path id="build.classpath">
<fileset file="lib/*.jar" />
</path>

<!-- Help -->
<target name="help">
<echo message="BASEDIR:${basedir}"/>
<echo message="targer: help"/>
<echo message="usage: ant "/>
</target>

<!-- Wsgen -->
<target name="Wsgen">
<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="build.classpath" />
<wsgen outputDirectory="bin" wsdl="http://192.168.0.16:8080/MathService/services/MathService?wsdl" package="client" />
</target>

</project>

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

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

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

public class TestClient {

public static void main(String[] args) {

MathServiceClient msc=new MathServiceClient();
MathServicePortType msp=msc.getMathServiceHttpPort();
System.out.println(msp.add(100, 200));
System.out.println(msp.sub(100, 200));
}

}

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

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

首先在项目XFireProject中增加一个build.xml文件。xfire提供了一个ant任务:
<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="myclasspath" />


build.xml文件的内容如下:

<?xml version="1.0"?>
<project name="XFireProject" default="genfiles" basedir=".">
<property name="lib" value="lib" />
<path id="myclasspath">
<fileset dir="${lib}">
<include name="*.jar" />
</fileset>
<pathelement location="${genfiles}" />
</path>
<!--通过XFire ant任务生成客户端代码的存放位置-->
<property name="code_path" value="src.client" />
<!--需要生成客户端代码的wsdl文件-->
<property name="wsdl_path" value="http://localhost:8080/xfire/services/HelloService?wsdl" />
<!--生成客户端代码的包名-->
<property name="code_package" value="com.liuxiang.xfire.client" />

<!-- Remove classes directory for clean build -->
<target name="clean" description="Prepare for clean build">
<delete dir="${code_path}"/>
<mkdir dir="${code_path}"/>
</target>

<!--<target name="genfiles" depends="clean" description="Generate the files"> -->
<target name="genfiles" description="Generate the files">
<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="myclasspath" />
<!--outputDirectory属性定义创建的代码所在的文件夹
wsdl是web服务的wsdl文件
package代表创建的代码的package
-->
<wsgen outputDirectory="${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文件。文件内容如下:

/** *//**
*
*/
package com.liuxiang.xfire;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import com.liuxiang.xfire.client.HelloServiceClient;
import com.liuxiang.xfire.client.HelloServicePortType;

/** *//**
* 通过XFire生成的客户端进行调用
*
* TestClient.java
* com.liuxiang.xfire
* XFireProject
* @author liuxiang mailto:juxtapose@163.com
* 2007-9-9 下午06:54:36
*
*/
public class TestClient ...{
/** *//**
* 客户端测试
* 通过ant脚本生成的客户端进行调用
*
* @param name 传入的参数,客户名字
* @return 返回sayHello()的返回值
*/
public static String testClient(String name)...{
HelloServiceClient helloSC = new HelloServiceClient();
HelloServicePortType helloSP = helloSC.getHelloServiceHttpPort();
String result = helloSP.sayHello(name);
return result;
}

/** *//**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception ...{
System.out.println(testClient("Liuxiang"));
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值