Dubbo源码之序列化Hessian

1.需求

          使用Dubbo中的Hessian进行序列化

2.代码

序列化类Student

public class Student implements java.io.Serializable{

    private String name;
    private int age;
    private String address;
    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;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", address="
                + address + "]";
    }
}

    使用Dubbo中的方法进行序列化

public class SerializeHessian {

    public static void main(String args[]){
        Student student = new Student();
        student.setAge(10);
        student.setName("村长");
        student.setAddress("杭州");

        byte[] results = null;
        ByteArrayOutputStream os = null;
        HessianSerializerOutput hessianOut = null;

        ByteArrayInputStream is = null;
        Student serStu = null;
        try{
            //进行序列化操作
            os = new ByteArrayOutputStream();
            hessianOut = new HessianSerializerOutput(os);
            hessianOut.writeObject(student);
            os.close();
            results = os.toByteArray();
            System.out.println(results);

            //反序列化操作
            is = new ByteArrayInputStream(results);
            HessianSerializerInput hessianInput = new HessianSerializerInput(is);
            serStu = (Student)hessianInput.readObject();

            System.out.println("-------反序列化数据----"+serStu);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

猜你喜欢

转载自my.oschina.net/u/1398304/blog/1809007