Dubbo serialization problem (3) The subclass overrides the parent class field hession deserialization cannot be obtained

I encountered a problem in the development of dubbo. When using hession2 for serialization, when the subclass and the parent class have the same field, the hession2 deserialization cannot obtain the field data, as follows:

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable{

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private String userId;
	private String userName;
	private Date addDate;
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public Date getAddDate() {
		return addDate;
	}
	public void setAddDate(Date addDate) {
		this.addDate = addDate;
	}
	
	
}

 

import java.util.Date;

public class ChildrenUser extends User {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private String userId;
	
	private Date addDate;

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public Date getAddDate() {
		return addDate;
	}

	public void setAddDate(Date addDate) {
		this.addDate = addDate;
	}

	@Override
	public String toString() {
		return "ChildrenUser [userId=" + userId + ", addDate=" + addDate + "]";
	}
	
}

 

The test procedure is as follows:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;

import com.alibaba.com.cauchess.hessian.io.Hessian2Input;
import com.alibaba.com.caucho.hessian.io.Hessian2Output;
import com.pinganwj.clinic.api.demo.domain.ChildrenUser;
import com.pinganwj.clinic.api.demo.domain.User;


public class TestHessionLite1 {


	public static void main(String[] args) throws IOException {
		User user=new ChildrenUser();
		user.setAddDate(new Date());
		user.setUserId("123");
		user.setUserName("Hehe");
		byte[] aa=TestHessionLite1.serialize(user);
		Object mm=TestHessionLite1.deserialize(aa);
		System.out.println(mm.toString());
		
	}
	
	public static byte[] serialize(Object obj) throws IOException{  
		 ByteArrayOutputStream os = new ByteArrayOutputStream();
		 Hessian2Output ho = new Hessian2Output(os);  
		 byte[] cc = null;
		try {
			if(obj==null) throw new NullPointerException();  
		    ho.writeObject(obj);
		    ho.flushBuffer();
		    cc=os.toByteArray();  
		} catch (Exception e) {
			e.printStackTrace ();
		}finally{
			ho.close();
		}
		return cc;  
	    
	}
	
	public static Object deserialize(byte[] by) throws IOException{
		try {
			if(by==null) throw new NullPointerException();  
			ByteArrayInputStream is = new ByteArrayInputStream(by);
		    Hessian2Input hi = new Hessian2Input(is);  
		    return hi.readObject();  
		} catch (Exception e) {
			e.printStackTrace ();
		}
		return null;
	    
	}  
}

 

Running the test program, the output is as follows

ChildrenUser [userId=null, addDate=null]

Then I use kryo to serialize,

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;

import org.apache.commons.codec.binary.Base64;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.JavaSerializer;
import com.pinganwj.clinic.api.demo.domain.ChildrenUser;
import com.pinganwj.clinic.api.demo.domain.User;

public class TestKryo1 {

	public static void main(String[] args) {

		User user=new ChildrenUser();
		user.setAddDate(new Date());
		user.setUserId("123");
		user.setUserName("Hehe");
		String aa=TestKryo1.serialize(user);
		Object mm=TestKryo1.deserialize(aa,User.class);
		System.out.println(mm.toString());
	}
	private static <T extends Serializable> String serialize(T obj) {
        Kryo kryo = new Kryo ();
        kryo.setReferences (false);
        kryo.register(obj.getClass(), new JavaSerializer());
 
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Output output = new Output(baos);
        kryo.writeClassAndObject(output, obj);
        output.flush();
        output.close();
 
        byte[] b = baos.toByteArray();
        try {
            baos.flush();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace ();
        }
 
        return new String(new Base64().encode(b));
    }
 
    @SuppressWarnings("unchecked")
    private static <T extends Serializable> T deserialize(String obj,
            Class<T> clazz) {
        Kryo kryo = new Kryo ();
        kryo.setReferences (false);
        kryo.register(clazz, new JavaSerializer());
 
        ByteArrayInputStream bais = new ByteArrayInputStream(
                new Base64().decode(obj));
        Input input = new Input(bais);
        return (T) kryo.readClassAndObject(input);
    }
}

 

The output is as follows:

ChildrenUser [userId=123, addDate=Fri Aug 25 00:28:45 CST 2017]

 The output is correct.

 

This is a pit of hession2. After viewing the readObjectInstance method in the source Hessian2Input class

He reads the properties of the parent class



 

And every time the value can be read for the first time, and then the second read is null, it is overwritten, so the output is null.

 

So the solution is to remove the fields in the subclass or the fields in the parent class, or use other serialization methods such as kryo instead.

Guess you like

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