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

猜你喜欢

转载自blog.csdn.net/afei8080/article/details/128342603
今日推荐