Serialized Hessian of Dubbo source code

1. Demand

          Serialization with Hessian in Dubbo

2. Code

Serialization class 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 + "]";
    }
}

    Serialize using methods in Dubbo

public class SerializeHessian {

    public static void main(String args[]){
        Student student = new Student();
        student.setAge(10);
        student.setName("Village Chief");
        student.setAddress("Hangzhou");

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

        ByteArrayInputStream is = null;
        Student serStu = null;
        try{
            // do serialization
            os = new ByteArrayOutputStream();
            hessianOut = new HessianSerializerOutput(os);
            hessianOut.writeObject(student);
            os.close();
            results = os.toByteArray();
            System.out.println(results);

            // deserialize operation
            is = new ByteArrayInputStream(results);
            HessianSerializerInput hessianInput = new HessianSerializerInput(is);
            serStu = (Student)hessianInput.readObject();

            System.out.println("-------Deserialize data----"+serStu);
        }catch(Exception e){
            e.printStackTrace ();
        }
    }

}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325894511&siteId=291194637