聲明:相關內容是根據網絡資料整理所得,僅供參考。java
基於XFire的WS服務端配置資料請自行到網上查找(也可以使用eclipse自帶工具很簡單的哦)。
網絡
服務端主要代碼:
eclipse
//WS接口 package test; public interface IHelloWorld { public String hello(String message); public String hello(User user); public User update(User user); } //WS接口實現 package test; public class HelloWorldImpl implements IHelloWorld { public String hello(String message) { System.out.println("server : " + message); return message; } public String hello(User user) { System.out.println("server : " + user.toString()); return user.toString(); } public User update(User user) { user.setName("server - " + user.getName()); user.setAge(user.getAge() * 2); return user; } } //實體類 public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "name=" + name + ",age=" + age; } } 客戶端主要代碼: package test; import java.net.URL; import org.codehaus.xfire.client.Client; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import test2.HelloWorldPortType; /** * @author Kind Cao * @version $Rev: $, May 7, 2012 2:59:03 PM */ public class HelloWorldClient { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { User user = new User(); user.setName("Kind"); user.setAge(20); // /////////////////////////////// /** * 1.動態客戶端 - Dynamic Client * <li>優勢:簡單易用 * <li>缺點:NA */ String wsdlUrl = "http://127.0.0.1:8080/testws/services/HelloWorld?wsdl"; Client client = new Client(new URL(wsdlUrl)); // invoke第一個參數:service方法名稱 // invoke第一個參數:方法參數列表(僅限簡單數據類型) Object[] obj = client.invoke("hello", new Object[] { "haha" }); System.out.println("Dynamic Client 0 - " + obj[0]); // 參數爲對象 Object[] obj2 = client.invoke("hello", new Object[] { user }); System.out.println("Dynamic Client 2 - " + obj2[0]); // Object[] obj3 = client.invoke("update", new Object[] { user }); System.out.println("Dynamic Client 3 - " + obj3[0]); /** * 2.代理工廠方法 - Proxy Factory * <li>優勢:NA * <li>缺點:須要服務端的接口Class和Entity類及aegis配置文件 */ String serviceURL = "http://127.0.0.1:8080/testws/services/HelloWorld"; Service serviceModel = new ObjectServiceFactory().create(IHelloWorld.class); IHelloWorld client2 = (IHelloWorld) new XFireProxyFactory().create(serviceModel, serviceURL); System.out.println("Proxy Factory 0 - " + client2.hello("hihi")); // 參數爲對象 System.out.println("Proxy Factory 2 - " + client2.hello(user)); System.out.println("Proxy Factory 3 - " + client2.update(user)); /** * 3.客戶端/服務端Stubs - 根據WSDL生成Client Stub * <li>優勢:NA * <li>缺點:相對比較複雜,須要根據wsdl生成客戶端的Stub */ test2.HelloWorldClient client3 = new test2.HelloWorldClient(); HelloWorldPortType port = client3.getHelloWorldHttpPort(); System.out.println("Client Stub 0 - " + port.hello("ahah")); // 參數爲對象 System.out.println("Client Stub 2 - " + port.hello1(user)); System.out.println("Client Stub 3 - " + port.update(user)); } }
備註:test2.HelloWorldClient 和 HelloWorldPortType 請自行用eclipse的xfire插件生成(http://xueyong.iteye.com/blog/65783),很簡單的哦。
ide
輸出結果:工具
Dynamic Client 0 - haha Dynamic Client 2 - name=Kind,age=20 Dynamic Client 3 - name=server - Kind,age=40 Proxy Factory 0 - hihi Proxy Factory 2 - name=Kind,age=20 Proxy Factory 3 - name=server - Kind,age=40 Client Stub 0 - ahah Client Stub 2 - name=Kind,age=20 Client Stub 3 - name=server - Kind,age=40