XFire访问WS的三种形式(JAVA版)

声明:相关内容是根据网络资料整理所得,仅供参考。

基于XFire的WS服务端配置资料请自行到网上查找(也可使用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),很简单的哦。 

输出结果:

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


转载于:https://my.oschina.net/kind790/blog/56634

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值