import java.io.*;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import hessian.Employee; public class HessianSerializeDeserializeMain {
/**
* Hessian实现序列化
* @param employee
* @return
* @throws IOException
*/
public static byte[] serialize(Employee employee){
ByteArrayOutputStream byteArrayOutputStream = null;
HessianOutput hessianOutput = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
// Hessian的序列化输出
hessianOutput = new HessianOutput(byteArrayOutputStream);
hessianOutput.writeObject(employee);
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
hessianOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
} /**
* Hessian实现反序列化
* @param employeeArray
* @return
*/
public static Employee deserialize(byte[] employeeArray) {
ByteArrayInputStream byteArrayInputStream = null;
HessianInput hessianInput = null;
try {
byteArrayInputStream = new ByteArrayInputStream(employeeArray);
// Hessian的反序列化读取对象
hessianInput = new HessianInput(byteArrayInputStream);
return (Employee)hessianInput.readObject();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
byteArrayInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
hessianInput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
} public static void main(String[] args) throws IOException {
// doSerialize();
// System.out.println(serialize);
// // 反序列化
// Employee deserialize = deserialize(serialize);
// System.out.println(deserialize.toString());
deSerialize();
} public static void doSerialize() throws IOException {
Employee employee = new Employee();
employee.setEmployeeId(1);
employee.setEmployeeName("lcc");
// employee.setAge("nan");
// 序列化
byte[] serialize = serialize(employee);
File file=new File("./serialize");
if (!file.exists()){
file.createNewFile();
}
System.out.println(String.valueOf(serialize));
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(file));
bufferedOutputStream.write(serialize);
bufferedOutputStream.flush();
} public static void deSerialize() throws IOException {
File file=new File("./serialize");
FileInputStream fileInputStream=new FileInputStream(file);
BufferedInputStream bufferedInputStream=new BufferedInputStream(fileInputStream);
byte result[]=new byte[1024];
if (bufferedInputStream.read(result)>result.length){
System.out.println("too small");
}
// 反序列化
Employee deserialize = deserialize(result);
System.out.println(deserialize.toString());
} }

Employee:

public class Employee {
private int employeeId;
private String employeeName;
private String age; public int getEmployeeId() {
return employeeId;
} public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
} public String getEmployeeName() {
return employeeName;
} public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} @Override
public String toString() {
return "Employee{" +
"employeeId=" + employeeId +
", employeeName='" + employeeName + '\'' +
", age='" + age + '\'' +
'}';
}
}

我们现进行序列化将结果写道文件中,在Employee中新增test字段,反序列化结果如下:

将Employee中的test和age去掉结果如下:

都是可以成功反序列化的

测试Hessian反序反序列化 客户端少字段和多字段时能否成功的更多相关文章

  1. 前端总结·基础篇·JS(二)数组深拷贝、去重以及字符串反序和数组(Array)

    目录 这是<前端总结·基础篇·JS>系列的第二篇,主要总结一下JS数组的使用.技巧以及常用方法. 一.数组使用 1.1 定义数组 1.2 使用数组 1.3 类型检测 二.常用技巧 2.1 ...

  2. C# 序列化与反序列化之xml对属性或者字段的子类化的子对象进行序列化的解决方案

    C# 序列化与反序列化之xml对属性或者字段的子类化的子对象进行序列化的解决方案 xml序列化涉及到XmlRoot,XmlInclude,XmlElement,XmlAttribute,XmlType ...

  3. 找一个四位数,要求该四位数的四倍刚好是该四位数的反序。 即b1b2b3b4 * 4 = b4b3b2b1

    找一个四位数,要求该四位数的四倍刚好是该四位数的反序. 即b1b2b3b4 * 4 = b4b3b2b1 解: 第一步,确认最末位 假设 b1b2b3b4 + b4b3b2b1 = [x0]x1x2x ...

  4. Django的列表反序

    Django虽然是python的web框架,但它不是所有的python特性都支持的. 最近在项目中遇到一个问题,需要在Django中将获得的列表反序排列,一开始我使用的是python的reverse方 ...

  5. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  6. pojg2744找一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是y的子串,或者x中的字符反序之后得到的新字符串是y的子串。

    http://poj.grids.cn/practice/2744 描述现在有一些由英文字符组成的大小写敏感的字符串,你的任务是找到一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是 ...

  7. 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出。

    namespace test2 { class Program { /// <summary> /// 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的 ...

  8. Excel反序排列

    实际工作中有这样一个需求,将Excel列表中所有的条目进行反序排列,有人说这还不简单直接选中某一列按照这列排序(升序或降序)就可以了. 但问题是这里没有可以参考的列,进行排序. 比如: 想转换为: 那 ...

  9. SortedDictionary<TKey,TValue>正序与反序排序及Dicttionary相关

    SortedDictionary<TKey,TValue>能对字典排序 using System; using System.Collections.Generic; using Syst ...

随机推荐

  1. Java实现评论回复功能

    目录 一.分类方式 1.单一型 2.嵌套型 3.两层型 二.实现原理 1.单一型 2.嵌套型 3.两层型 使用递归循环开发评论回复功能,适用于大部分的简单单体应用 评论功能或许是大多数的单体应用之中会 ...

  2. django 框架模型之models常用的Field

    1. django 模型models 常用字段          1.models.AutoField 自增列 = int(11) 如果没有的话,默认会生成一个名称为 id 的列 如果要显式的自定义一 ...

  3. Spring Cloud Alibaba 基础

    Spring Cloud Alibaba 基础 什么是Spring Cloud Alibaba 这里我们不讲解Spring Cloud 和 Spring Cloud Alibaba 的关系,大家自己查 ...

  4. Centos中部署NetCore项目(二)

    前言 在centos中部署程序,一般都不会是使用控制台进程直接启动,或者是后台运行.而是一些守护进程管理工具进行管理,例如supervisor. 部署Web相关程序,使用nginx是比较普遍的, 安装 ...

  5. SSM使用Ueditor

    富文本编辑器(UEditor) 1. 下载UEditor富文本编辑器 建议下载 utf8-jsp 版本的,结构目录如下: 下载地址:链接:https://pan.baidu.com/s/1Nq0oJB ...

  6. iNeuOS工业互联平台,WEB组态(iNeuView)增加工程视图导入、导出功能,及优化和修复,发布:v3.2.1版本

    目       录 1.      概述... 2 2.      平台演示... 2 3.      导出组态工程文件... 2 4.      导入组态工程文件... 3  1.   概述 iNe ...

  7. c#视频位置

    static void Main(string[] args)        { string scoure = @"C:\Documents and Settings\Administra ...

  8. 5.MVCC

    5 MVCC ​ 全称是Multi-Version Concurrent Control,即多版本并发控制,在MVCC协议下,每个读操作会看到一个一致性的snapshot,并且可以实现非阻塞的读.MV ...

  9. 初次使用flask

    以写的一个小的例子来记录第一次使用: from flask import Flask, render_template import json # 实例化,可视为固定格式 app = Flask(__ ...

  10. 十个Pycharm快捷键——提升效率

    一些比较实用的Pycharm的快捷键,提升编写开发效率. 1.解除语法限制 默认情况下,Pycharm会对代码进行检查,包括但不仅限于代码是否有语法错误,是否符合PEP8规范. 如命名检查,如下图 变 ...