Hessian序列化复杂对象

1、创建复杂类:
package lavasoft.suths.pojo; 

import java.io.Serializable; 
import java.util.Date; 

/** 
* 一个象征性的复杂类型 

* @author leizhimin 2009-8-14 17:21:40 
*/
 
public  class Foo  implements Serializable { 
         private  static  final  long serialVersionUID = 1792241905841405420L; 
         private String name; 
         private Date createtime; 

         public Foo(String name) { 
                 this.name = name; 
                createtime =  new Date(); 
        } 

         public String getName() { 
                 return name; 
        } 

         public  void setName(String name) { 
                 this.name = name; 
        } 

         public Date getCreatetime() { 
                 return createtime; 
        } 

         public  void setCreatetime(Date createtime) { 
                 this.createtime = createtime; 
        } 

        @Override 
         public String toString() { 
                 return  "Foo{" + 
                                 "name='" + name + '\'' + 
                                 ", createtime=" + createtime + 
                                '}'; 
        } 
}
 
2、修改服务接口和实现
public  interface Hello { 
        String sayHello(String name); 

        Foo makeFoo(String name); 
}
 
public  class HelloService  implements Hello { 
         public String sayHello(String name) { 
                 return  "Hello " + name +  "!"
        } 

         public Foo makeFoo(String name) { 
                 return  new Foo(name); 
        } 
}
 
3、部署web,其他的配置都不用改变,并运行tomcat。
 
4、写客户端测试:
/** 
* 客户端调用(会依赖服务接口) 

* @author leizhimin 2009-8-14 12:29:33 
*/
 
public  class Client { 
         public  static  void main(String[] args)  throws MalformedURLException { 
                String url =  "http://localhost:8080/hessianapp/hessian/hello"; 
                HessianProxyFactory factory = new HessianProxyFactory(); 
                Hello hello = (Hello) factory.create(Hello.class, url); 
                System.out.println(hello.sayHello("Hessian")); 
                System.out.println(hello.makeFoo("foo")); 
        } 
}
 
运行结果:
Hello Hessian! 
Foo{name='foo', createtime=Fri Aug 14 17:32:17 CST 2009} 

Process finished with exit code 0
 
整合Spring的测试:
/** 
* Spring整合Hessian,客户端测试 

* @author leizhimin 2009-8-14 15:32:46 
*/
 
public  class TestClient { 
         public  static  void main(String[] args) { 
                 try { 
                        ApplicationContext context =  new ClassPathXmlApplicationContext( "/remoting-client.xml"); 
                        Hello hello = (Hello) context.getBean( "helloServiceClient"); 
                        System.out.println(hello.sayHello( "Spring Hession")); 
                        System.out.println(hello.makeFoo( "SpringFoo")); 
                } 
                 catch (Exception e) { 
                        e.printStackTrace(); 
                } 
        } 
}
 
运行结果:
Hello Spring Hession! 
Foo{name='SpringFoo', createtime=Fri Aug 14 17:41:29 CST 2009} 

Process finished with exit code 0
 
总结
1、Hessian的序列化做的还不错,可以信赖。
2、序列化对象要实现java.io.Serializable接口。
3、序列化类要有serialVersionUID。
4、注意序列化的规范,有些成员是不能序列化的。


本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/191889,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值