ITeye博客

XFire的Web Servicep客户端快速配置

pingwei000 · 2010-06-07 14:41 · 企业架构 · Markdown

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

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

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

 <path id="build.classpath">
     <fileset file="lib/*.jar" />
 </path>
 <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>

二、运行通过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任务:

build.xml文件的内容如下:

<!-- 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>

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)

暂无评论。

评论功能维护中,当前仅展示历史评论。