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. 每日10句:day1

    1,plt.style.use('ggplot') #使用R语言的图像配色方案 2,for a,b in zip(x,y): plt.text(a,b+1,'%.0f'%b,ha='center',v ...

  2. js——事件循环

    JS-事件循环 js运行的环境称之为宿主环境. 执行栈 :call stack ,一个数据结构,用于存放各种函数的执行环境,每一个函数执行之前他的相关信息会加入到执行栈中,函数调用之前,创建执行环境, ...

  3. linux添加自动清空缓存

    1. cleanCache.sh vim cleanCache.sh #!/bin/bash #每两小时清除一次缓存 echo "开始清除缓存" sync;sync;sync #写 ...

  4. 【Kata Daily 190929】Password Hashes(密码哈希)

    题目: When you sign up for an account somewhere, some websites do not actually store your password in ...

  5. DM的SQL优化入门笔记

    1.查看执行计划EXPLAIN SELECT A.C1+1,B.D2 FROM T1 A, T2 B WHERE A.C1 = B.D1; 2.执行计划: 1 #NSET2: [0, 16, 9] 2 ...

  6. 19、Haystack

    Haystack 1.什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsear ...

  7. Java设计系列之书店管理系统单机版

    书店管理系统: 项目练习目标 :1.Java应用程序基本分析2.培养面向对象编程的基本思想3.Java基本设计模式综合应用4.掌握分层和接口的基本设计5.构建合理的Java应用程序包结构6.综合应用J ...

  8. 中介者模式及在NetCore中的使用MediatR来实现

    在现实生活中,常常会出现好多对象之间存在复杂的交互关系,这种交互关系常常是"网状结构",它要求每个对象都必须知道它需要交互的对象.例如,每个人必须记住他(她)所有朋友的电话:而且, ...

  9. 对udp dns的一次思考

    目前昨天查一个线上问题:""dns服务器在我们的设备, 有大量的终端到设备上请求解析域名,但是一直是单线程,dns报文处理不过来", 然而设备是多核,dns服务器一直不能 ...

  10. 《GNU_makefile》第七章——makefile的条件执行

    条件执行即,通过变量的值,来控制make的执行和忽略. 条件执行只能控制makefile的make语法部分,不能控制shell部分 1.一个例子 - libs_for_gcc = -lgnu norm ...