Hessian序列化

2018-06-28  本文已影响0人  WY_250e
public class Test {
    public static void main(String[] args) throws Exception {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Hessian2Output output = new Hessian2Output(os);
        output.writeObject(Person.hehe(123L, "wangyong"));
        output.close();

        ByteArrayInputStream in = new ByteArrayInputStream(os.toByteArray());
        Hessian2Input input = new Hessian2Input(in);
        System.out.println(input.readObject());
    }
}

class Person implements Serializable {
    private Long id;
    private String name;
    private Person(long id, String name) {
        this.id = id;
        this.name = name;
        System.out.println("call dd");
    }

    public static Person hehe(Long id, String name) {
        Person p = new Person(id, name);
        return p;
    }

    @Override
    public String toString() {
        return "id=" + id + ", name=" + name;
    }
}

源码参看dubbo 2.6.1版本自带的hessian框架
Hessian序列化的原理是利用反射,
反序列化时:找到一个性能最高的contructor,入参基本类型传0、false等,引用类型传null(所以如果contructor中判断参数不能为null,则会报错) . contructor可为private

使用示例
源码阅读

hessian和java自带序列化区别

http://www.cnblogs.com/wzyxidian/p/5726584.html
https://blog.csdn.net/chen_fly2011/article/details/56664712

上一篇下一篇

猜你喜欢

热点阅读