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、注意序列化的规范,有些成员是不能序列化的。