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]);
}
}