声明:相关内容是根据网络资料整理所得,仅供参考。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