1、实现序列化及反序列化的目的主要是为了实现网络中数据传输,hessian可以将对象序列化为二进制数据流,降低网络带宽,实现更高效的传输。
2、使用hessian实现序列化的对象要求必须实现java.io.Serializable接口,否则运行会报错。
3、我们来看一下具体的代码实现,首先自定义一个User对象:

public class User implements Serializable {

    private String name;

    private int age;

    private Date birthday;

    private double weight;

    private List<String> favi = new ArrayList<>();
    ......

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("name=").append(this.name).append(",");
        sb.append("age=").append(this.age).append(",");
        sb.append("birthday=").append(DateFormatUtils.format(this.birthday, "yyyy-MM-dd")).append(",");
        sb.append("weight=").append(this.weight).append(",");
        sb.append("favi=");
        for(String s : this.favi){
            sb.append(s).append(",");
        }
        return sb.toString();
    }
}

序列化及反序列化方法:

/**
 * hessian序列化及反序列化
 * @author 米酒
 * @create 2020/1/5 11:58
 */
public final class HessianSerializeUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(HessianSerializeUtil.class);

    /**
     * 序列化
     * @author 米酒
     * @date 2020/1/5 12:01
     * @param t
     * @return byte[]
     */
    public static <T extends Serializable> byte[] serialize(T t){
        HessianOutput hessianOutput = null;
        try(ByteArrayOutputStream os = new ByteArrayOutputStream()){
            hessianOutput = new HessianOutput(os);
            hessianOutput.writeObject(t);
            return os.toByteArray();
        }catch(Exception e){
            LOGGER.error("serialize", e);
        }finally {
            if(hessianOutput!=null){
                try {
                    hessianOutput.close();
                } catch (IOException e) {
                    LOGGER.error("serialize", e);
                }
            }
        }
        return null;
    }

    /**
     * 反序列化
     * @author 米酒
     * @date 2020/1/5 12:33
     * @param bytes
     * @return T
     */
    public static <T extends Serializable> T deserialize(byte[] bytes){
        HessianInput hessianInput = null;
        try(ByteArrayInputStream is = new ByteArrayInputStream(bytes)){
            hessianInput = new HessianInput(is);
            return (T) hessianInput.readObject();
        }catch(Exception e){
            LOGGER.error("deserialize", e);
        }finally {
            if(hessianInput!=null){
                hessianInput.close();
            }
        }
        return null;
    }

    /**
     * 测试
     */
    public static void main(String[] args) {
        User user = new User();
        user.setName("张三");
        user.setAge(19);
        user.setBirthday(new Date());
        user.setWeight(75.5);
        user.addFavi("跑步");
        user.addFavi("游泳");
        System.out.println("序列化前:");
        System.out.println(user.toString());

        byte[] bytes = serialize(user);
        User u = deserialize(bytes);
        System.out.println("反序列化后:");
        System.out.println(u.toString());
    }
}

最后看一下运行结果:

序列化前:
name=张三,age=19,birthday=2020-01-05,weight=75.5,favi=跑步,游泳,
反序列化后:
name=张三,age=19,birthday=2020-01-05,weight=75.5,favi=跑步,游泳,

发表评论