Complete the serialization and deserialization of objects based on Hession

Step01 : Define the mail class:

public class Mail implements Serializable{
	private static final long serialVersionUID = 6599166688654530165L;
	private Integer id;
	private String title;
	private String content;
	private Date createdTime;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Date getCreatedTime() {
		return createdTime;
	}
	public void setCreatedTime(Date createdTime) {
		this.createdTime = createdTime;
	}
	@Override
	public String toString() {
		return "Mail [id=" + id + ", title=" + title + ", content=" + content + ", createdTime=" + createdTime + "]";
	}
}

Step02 : Add dependency

<dependency>
	<groupId>com.caucho</groupId>
	<artifactId>hessian</artifactId>
	<version>4.0.60</version>
</dependency>

Step03 : Write the test class, based on the documentation, to achieve serialization and deserialization, please refer to the official:

http://hessian.caucho.com/doc/hessian-overview.xtp

Add serialization and deserialization methods to the test class to complete the serialization and deserialization operations. Create the test class TestSerializable03, and then add the object serialization method definition

private static void doSerialization(Object obj) throws FileNotFoundException, IOException {
		OutputStream os = new FileOutputStream("test.xml");
		Hessian2Output out = new Hessian2Output(os);
		out.writeObject(obj);
		out.flush();
		os.close();
}

Add object deserialization method definition

private static Object doDeserialization() throws FileNotFoundException, IOException {
		InputStream is = new FileInputStream("test.xml");
		Hessian2Input in = new Hessian2Input(is);
		Object obj2 = in.readObject();
		is.close();
		return obj2;
}

Add the main method for testing:

public static void main(String[] args)throws Exception {
		Mail obj = new Mail();
		obj.setId(200);
		obj.setTitle("test");
		obj.setContent("this is test content");
		obj.setCreatedTime(new Date());
		//序列化
	        doSerialization(obj);
		//反序列化
		Mail obj2 =(Mail)doDeserialization();
		System.out.println(obj2);
	}

Description: The above serialization method can be encapsulated and written into the serialization tool class, for example

public class SerializationUtil {
	//反序列化
	@SuppressWarnings("unchecked")
	public static <T>T doDeserialization(String file) throws FileNotFoundException, IOException {
		InputStream is = new FileInputStream(file);
		Hessian2Input in = new Hessian2Input(is);
		T t =(T)in.readObject();
		is.close();
		return t;
	}
	//序列化
	public static <T extends Serializable>void 
	     doSerialization(T obj,String file) throws FileNotFoundException, IOException {
		OutputStream os = new FileOutputStream(file);
		Hessian2Output out = new Hessian2Output(os);
		out.writeObject(obj);
		out.flush();
		os.close();
	}
}

Based on Hessian to implement java object serialization, java classes still need to implement Serializable objects.

 

Guess you like

Origin blog.csdn.net/qianzhitu/article/details/103009083
Recommended