Hessian 反序列化找不到类时自动转换成map

该文章展示了使用Hessian进行序列化和反序列化的Java代码示例,包括Hessian2Input和Hessian2Output的使用。文中还提到了AntGroup的版权信息,并用Fastjson转换字节流到对象。测试中比较了序列化前后对象的表示形式。
摘要由CSDN通过智能技术生成

序列化,反序列化 实现的本质_fei33423 反序列化_个人渣记录仅为自己搜索用的博客-CSDN博客

论Hessian的各种坑爹骚操作 - 原理探究 - 掘金

/*
 * Ant Group
 * Copyright (c) 2004-2023 All Rights Reserved.
 */
package HessianSerializer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.SerializerFactory;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;

/**
 * @author admin
 * @version d.java, v 0.1 2023年04月27日 下午3:28 admin
 */
public class HessianSerializer {
    static HessianSerializer hessianSerializer = new HessianSerializer();
    SerializerFactory serializerFactory = new SerializerFactory();

    public static abstract class ToStringBase implements Serializable {
        private static final long serialVersionUID = 2160996531438393638L;

        public ToStringBase() {
        }

        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
        }
    }

    public static class TestParent extends ToStringBase implements Serializable  {
        TestChild testChild = new TestChild();
    }

    public static class TestChild extends ToStringBase implements Serializable {
        public String name = "1";

    }

    @Test
    public void test() throws IOException {
        //TestParent obj = new TestParent();
        //Map<String,Object> map=new HashMap();
        //map.put("kksd",obj);
        //
        //byte[] bytes = hessianSerializer.serializeObject(map);
        //ArrayList<Object> objects1 = new ArrayList<>();
        //
        //for (byte aByte : bytes) {
        //    objects1.add(aByte);
        //}
        //String x = JSON.toJSONString(objects1);

        String x = "[77,4,107,107,115,100,79,-56,85,99,111,109,46,97,108,105,112,97,121,46,102,105,110,109,111,100,101,108,99,111,114,101,"
                + "46,97,99,116,115,46,116,101,115,116,46,97,108,103,111,114,105,116,104,109,46,119,104,111,108,101,112,114,111,99,101,"
                + "115,115,46,72,101,115,115,105,97,110,83,101,114,105,97,108,105,122,101,114,36,84,101,115,116,80,97,114,101,110,116,"
                + "-111,9,116,101,115,116,67,104,105,108,100,111,-112,79,-56,84,99,111,109,46,97,108,105,112,97,121,46,102,105,110,109,"
                + "111,100,101,108,99,111,114,101,46,97,99,116,115,46,116,101,115,116,46,97,108,103,111,114,105,116,104,109,46,119,104,"
                + "111,108,101,112,114,111,99,101,115,115,46,72,101,115,115,105,97,110,83,101,114,105,97,108,105,122,101,114,36,84,101,"
                + "115,116,67,104,105,108,100,-111,4,110,97,109,101,111,-111,1,49,122]";
        System.out.println(x);
        List<Byte> maps = JSON.parseArray(x, Byte.class);
        byte[] bytes = new byte[maps.size()];

        for (int i = 0; i < maps.size(); i++) {
            bytes[i] = maps.get(i);
        }

        System.out.println(Arrays.toString(bytes));
        Map<String,Object> o = (Map<String,Object>) hessianSerializer.deserializeObject(bytes);
        System.out.println(o);
    }

    /**
     * 反序列化
     *
     * @param bytes
     * @return
     */
    public Object deserializeObject(byte[] bytes) {
        InputStream is = new ByteArrayInputStream(bytes);
        Hessian2Input h2in = new Hessian2Input(is);
        h2in.setSerializerFactory(serializerFactory);
        try {
            return h2in.readObject();
        } catch (Exception e) {
            // 简单异常处理,把CheckedException转换为RuntimeException
            throw new RuntimeException(e);
        } finally {
            try {
                h2in.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }

    /**
     * 序列化
     *
     * @param obj
     * @return
     */
    public byte[] serializeObject(Object obj) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Hessian2Output h2out = new Hessian2Output(os);
        h2out.setSerializerFactory(serializerFactory);
        try {
            h2out.writeObject(obj);
            h2out.flush();
        } catch (Exception e) {
            // 简单异常处理,把CheckedException转换为RuntimeException
            throw new RuntimeException(e);
        } finally {
            try {
                h2out.close();
            } catch (IOException e) {
                // ignore
            }
        }
        return os.toByteArray();
    }


}

第一遍执行

  {kksd=HessianSerializer.TestParent[testChild=HessianSerializer.TestChild[name=1]]}
 

第二遍,把类注释掉,x变成 "之前打印出来的byte流"

   {kksd={testChild={name=1}}}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值