1.本人也是参看了晁岳攀先生的XFire开发指南一书而总结的!反正俺也是新人,总结一下以后好用!哈哈
2.方式一:Ant(亲爱的蚂蚁工具)
<?xml version="1.0" encoding="UTF-8"?>
<project name="client" default="help" basedir=".">
<!-- =============================== -->
<!-- 设置属性 -->
<!-- =============================== -->
<property name="optimize" value="false" />
<property name="debug" value="on" />
<property name="deprecation" value="false" />
<!-- 第三方jar包 -->
<property name="build.lib" value="${basedir}/lib" />
<property name="sources" value="${basedir}/src" />
<!-- 编译后的class文件存储 -->
<property name="build.classes" value="${basedir}/bin" />
<!-- =============================== -->
<!-- 设置类路径 -->
<!-- =============================== -->
<path id="classpath">
<pathelement location="${build.classes}" />
<fileset dir="${build.lib}">
<include name="*.jar" />
</fileset>
</path>
<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"
classpathref="classpath" />
<!-- =============================== -->
<!-- 帮助信息 -->
<!-- =============================== -->
<target name="help" description="显示帮助信息">
<echo message="target描述" />
<echo message="-------------------------------------------" />
<echo message="compile 编译代码" />
<echo message="create_code 创建代码" />
</target>
<!-- =============================== -->
<!-- 编译代码 -->
<!-- =============================== -->
<target name="compile" description="编译代码">
<echo>编译程序代码</echo>
<javac srcdir="${sources}" destdir="${build.classes}"
classpathref="classpath" debug="${debug}" optimize="${optimize}"
deprecation="${deprecation}" />
</target>
<!-- =============================== -->
<!-- 创建客户端代码 -->
<!-- =============================== -->
<target name="create_code" description="创建代码">
<echo>创建代码</echo>
<wsgen outputDirectory="${sources}" wsdl="${basedir}/HelloService.wsdl"
package="org.joe.xfire.services" overwrite="true"/>
</target>
</project>
客户端调用(这里要把WSDL下载到classpath下)
/**
*
*/
package test;
import org.joe.xfire.services.HelloServiceClient;
import org.joe.xfire.services.HelloServicePortType;
/**
* @author joe
*
*/
public class ConsumeWebService {
/**
* @param args
* 使用创建的客户端代码访问HelloSerivce
*/
public static void main(String[] args) {
HelloServiceClient client = new HelloServiceClient();
HelloServicePortType helloService = client.getHelloServiceHttpPort();
//调用服务
String result = helloService.sayHello("joe");
System.out.println("结果: "+ result);
}
}
方式二:XFire plugin使用(一键生成)
下载地址:http://xfire.codehaus.org/Eclipse+Plugin
(详见附件图片)
客户端调用代码:
/**
*
*/
package client;
import org.joe.xfire.HelloServiceClient;
import org.joe.xfire.HelloServicePortType;
/**
* @author joe
*
*/
public class ConsumeWebService {
/**
* @param args
* 这个实例是由XFire plugin自动生成的
* 使用创建的客户端代码访问HelloSerivce
* 问题1:好像,如果使用plugin自带的jar的话,会遗失一些需要的jar
* 问题2:为什么不能像插件介绍那样,直接load进来WebServices的WSDL地址即可
*/
public static void main(String[] args) {
HelloServiceClient client = new HelloServiceClient();
HelloServicePortType helloService = client.getHelloServiceHttpPort();
//调用服务
String result = helloService.sayHello("endeavor");
System.out.println("结果: "+ result);
}
}
方式三:简单的Client代码:
/**
*
*/
package org.joe.webservices.xfire;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.client.Client;
/**
* @author joe
*
*/
public class DynamicClient {
/**
* @param args
* Simple client Demonstration
* @throws Exception
* @throws MalformedURLException
*/
public static void main(String[] args) throws MalformedURLException,
Exception {
//XFire client
Client client = new Client(new URL(
"http://localhost:8882/xfire.demo1/services/HelloService?wsdl"));
//第一个参数是方法名,后面的参数是需要传入的参数
Object[] results = client.invoke("sayHello", new Object[]{""});
System.out.println((String)results[0]);
}
}