Hessian序列化复杂对象
生活随笔
收集整理的這篇文章主要介紹了
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,如需轉載請自行聯系原作者
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
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,如需轉載請自行聯系原作者
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的Hessian序列化复杂对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 竞价账户烧钱的七大病因和处理办法
- 下一篇: 原创:荣耀怎么了?四曲屏手机突降1698