测试Hessian反序反序列化 客户端少字段和多字段时能否成功
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反序反序列化 客户端少字段和多字段时能否成功的更多相关文章
- 前端总结·基础篇·JS(二)数组深拷贝、去重以及字符串反序和数组(Array)
目录 这是<前端总结·基础篇·JS>系列的第二篇,主要总结一下JS数组的使用.技巧以及常用方法. 一.数组使用 1.1 定义数组 1.2 使用数组 1.3 类型检测 二.常用技巧 2.1 ...
- C# 序列化与反序列化之xml对属性或者字段的子类化的子对象进行序列化的解决方案
C# 序列化与反序列化之xml对属性或者字段的子类化的子对象进行序列化的解决方案 xml序列化涉及到XmlRoot,XmlInclude,XmlElement,XmlAttribute,XmlType ...
- 找一个四位数,要求该四位数的四倍刚好是该四位数的反序。 即b1b2b3b4 * 4 = b4b3b2b1
找一个四位数,要求该四位数的四倍刚好是该四位数的反序. 即b1b2b3b4 * 4 = b4b3b2b1 解: 第一步,确认最末位 假设 b1b2b3b4 + b4b3b2b1 = [x0]x1x2x ...
- Django的列表反序
Django虽然是python的web框架,但它不是所有的python特性都支持的. 最近在项目中遇到一个问题,需要在Django中将获得的列表反序排列,一开始我使用的是python的reverse方 ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- pojg2744找一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是y的子串,或者x中的字符反序之后得到的新字符串是y的子串。
http://poj.grids.cn/practice/2744 描述现在有一些由英文字符组成的大小写敏感的字符串,你的任务是找到一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是 ...
- 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出。
namespace test2 { class Program { /// <summary> /// 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的 ...
- Excel反序排列
实际工作中有这样一个需求,将Excel列表中所有的条目进行反序排列,有人说这还不简单直接选中某一列按照这列排序(升序或降序)就可以了. 但问题是这里没有可以参考的列,进行排序. 比如: 想转换为: 那 ...
- SortedDictionary<TKey,TValue>正序与反序排序及Dicttionary相关
SortedDictionary<TKey,TValue>能对字典排序 using System; using System.Collections.Generic; using Syst ...
随机推荐
- Linux下如何使用X86 CPU的GPIO
目录 1.前言 2.linux pinctrl子系统 3. pin controller driver 4.手动构造device 1.前言 在arm嵌入式开发中,各个外设具有固定的物理地址,我们可以直 ...
- 【开源】使用.Net Core和GitHub Actions实现哔哩哔哩每日自动签到、投币、领取奖励
BiliBiliTool是一个B站自动执行任务的工具,使用.NET Core编写,通过它可以实现B站帐号的每日自动观看.分享.投币视频,获取经验,每月自动领取会员权益.自动为自己充电等功能,帮助我们轻 ...
- python之冒泡排序改进
冒泡排序改进 关注公众号"轻松学编程"了解更多. 一.普通冒泡排序 [22,3,1,6,7,8,2,5] 普通冒泡排序 思路: 第一趟排序 从下标0开始,取出对应的值22 22和3 ...
- 851. Loud and Rich —— weekly contest 87
851. Loud and Rich 题目链接:https://leetcode.com/problems/loud-and-rich/description/ 思路:有向图DFS,记录最小的quie ...
- 三分钟快速解析GraphQL基本工作思路!
欢迎阅读 本文会通过实际场景介绍一下 GraphQL,目的是让你快速了解 GraphQL 是什么,以及基本工作思路,不包含实际用法,所以阅读很轻松. 一.GraphQL 是什么? GraphQL 是后 ...
- Python正则表达式-换行的匹配
找到了之前参考的博文,用来记录一下https://www.cnblogs.com/baxianhua/p/8572805.html 平常 点 (.)去匹配任意字符的时候,是不能匹配换行符的 匹配换行: ...
- 力扣 - 146. LRU缓存机制
目录 题目 思路 代码 复杂度分析 题目 146. LRU缓存机制 思路 利用双链表和HashMap来解题 看到链表题目,我们可以使用头尾结点可以更好进行链表操作和边界判断等 还需要使用size变量来 ...
- 经典c程序100例==41--50
[程序41] 题目:学习static定义静态变量的用法 1.程序分析: 2.程序源代码: #include "stdio.h" varfunc() { int var=0; sta ...
- JS中的Array之方法(1)
a=[2,4,5,6,7,90]; [1]. a.toString(); // 返回字符串表示的数组,逗号分隔 "2,4,5,6,7,90" [2]. a.join('||'); ...
- Docker - 解决重新进入容器后,环境变量失效的问题
问题背景 在容器中,在 /etc/profile . /etc/environment 设置了环境变量 退出容器,重新进入容器 刚刚设置的环境变量失效了 解决办法 将环境变量设置在 /root/.ba ...