springboot simple (11) springboot protostuff

1 protostuff简介

protostuff是一个基于protobuf实现的序列化方法。

优点:不用写.proto文件。

2 springboot protobuf集成开发

第1步:pom文件引入:

       <dependency>
			<groupId>io.protostuff</groupId>
			<artifactId>protostuff-core</artifactId>
			<version>1.4.0</version>
		</dependency>
		<dependency>
			<groupId>io.protostuff</groupId>
			<artifactId>protostuff-runtime</artifactId>
			<version>1.4.0</version>
		</dependency>

第2步:序列化工具类:serializeUtil:

public class serializeUtil {
    public static byte[] serializeObjToByte(Heroic heroic) {
        if (heroic == null) {
            return null;
        }
        Schema<Heroic> schema = RuntimeSchema.getSchema(Heroic.class);
        LinkedBuffer buffer = LinkedBuffer.allocate(4096);
        byte[] protostuff = null;
        try {
            protostuff = ProtostuffIOUtil.toByteArray(heroic, schema, buffer);
        } finally {
            buffer.clear();
        }
        return protostuff;
    }

    public static List<byte[]> serializeListToByte(List<Heroic> list) {
        if (list == null || list.size() <= 0) {
            return null;
        }
        List<byte[]> bytes = new ArrayList<>();
        Schema<Heroic> schema = RuntimeSchema.getSchema(Heroic.class);
        LinkedBuffer buffer = LinkedBuffer.allocate(4096);
        byte[] protostuff = null;
        for (Heroic heroic : list) {
            try {
                protostuff = ProtostuffIOUtil.toByteArray(heroic, schema, buffer);
                bytes.add(protostuff);
            } finally {
                buffer.clear();
            }
        }
        return bytes;
    }

    public static Heroic deserializeByteToObj(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        Schema<Heroic> schema = RuntimeSchema.getSchema(Heroic.class);
        List<Heroic> list = new ArrayList<Heroic>();

        Heroic heroic = new Heroic();
        ProtostuffIOUtil.mergeFrom(bytes, heroic, schema);
        return heroic;
    }

    public static List<Heroic> deserializeByteToList(
            List<byte[]> bytesList) {
        if (bytesList == null || bytesList.size() <= 0) {
            return null;
        }
        Schema<Heroic> schema = RuntimeSchema.getSchema(Heroic.class);
        List<Heroic> list = new ArrayList<Heroic>();
        for (byte[] bs : bytesList) {
            Heroic product = new Heroic();
            ProtostuffIOUtil.mergeFrom(bs, product, schema);
            list.add(product);
        }
        return list;
    }
}

3 测试验证

postman发送请求:
在这里插入图片描述

序列化byte后:
在这里插入图片描述
反序列化后:
在这里插入图片描述
代码详见:
https://gitee.com/linghufeixia/springboot-simple
chapter8-2


教程列表
springboot simple(0) springboot简介
springboot simple(1) springboot Helloworld
springboot simple(2) springboot Starter
springboot simple(3 )springboot Web开发
springboot simple(4)springboot 数据持久化
springboot simple (5) springboot Nosql
springboot simple (6) springboot mqtt
springboot simple (7) springboot thrift
springboot simple (8) springboot kafka
springboot simple (9) springboot jpa(Hibernate)
springboot simple (10) springboot protobuf
springboot simple (11) springboot protostuff
springboot simple (12) springboot RabbitMQ

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值