IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> springboot simple (11) springboot protostuff -> 正文阅读

[Java知识库]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

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-12-25 10:52:39  更:2022-12-25 10:57:42 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/26 10:48:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码