• JAVA 调用 com.google.protobuf


    1、pom.xml引入依赖

          <dependency>
              <groupId>com.google.protobuf</groupId>
              <artifactId>protobuf-java</artifactId>
              <version>3.17.3</version>
        </dependency>
            
        <dependency>
              <groupId>com.google.protobuf</groupId>
              <artifactId>protobuf-java-util</artifactId>
              <version>3.17.3</version>
        </dependency>

    2、到 https://github.com/protocolbuffers/protobuf/releases/tag/v3.17.3 下载相应版本的"protoc.exe" ,在 protoc-3.17.3-win64.zip 压缩包里;

    3、创建 .proto 文件StockInfoReply.proto,内容示例:

    syntax = "proto3";
    option java_package = "com.xrh.pb";
    option java_outer_classname = "StockInfoReplyModel";
    
    message StockInfoReply {
    
      string Symbol = 1;
      string Symbol_Name = 2;
      string PinYin_Name = 3;
      string Full_Name = 4;
      string Eng_Name = 5;
      string Exchange= 6;
      string CurrencyType = 7;
      string Industry = 8;
      string Regional = 9;
      string SymbolType = 10;
      string Concept = 11;
      string Market = 12;
      string ListStatus = 13;
        
    }

    4、DOS文件定位到 protoc.exe 所在目录,执行生成JAVA文件命令:

    protoc.exe --java_out=D:projectxrh_springbootsrcmainjava StockInfoReply.proto

    5、文件目录结构如下:

    6、测试MyTest.java代码:

    package com.xrh.pb;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    
    public class MyTest {
    
        public static void main(String[] args) {
            
            // 按照定义的数据结构,创建一个Person
            StockInfoReplyModel.StockInfoReply.Builder personBuilder = StockInfoReplyModel.StockInfoReply.newBuilder();
            personBuilder.setSymbol("00001");
            personBuilder.setSymbolName("上证指数");
            StockInfoReplyModel.StockInfoReply xxg = personBuilder.build();
            
            // 将数据写到输出流,如网络输出流,这里就用ByteArrayOutputStream来代替
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            try {
                xxg.writeTo(output);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            // -------------- 分割线:上面是发送方,将数据序列化后发送 ---------------
            
            byte[] byteArray = output.toByteArray();
            
            // -------------- 分割线:下面是接收方,将数据接收后反序列化 ---------------
            
            // 接收到流并读取,如网络输入流,这里用ByteArrayInputStream来代替
            ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
            
            // 反序列化
            StockInfoReplyModel.StockInfoReply xxg2;
            try {
                xxg2 = StockInfoReplyModel.StockInfoReply.parseFrom(input);
                System.out.println("Symbol:" + xxg2.getSymbol());
                System.out.println("SymbolName:" + xxg2.getSymbolName());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    李小家
  • 相关阅读:
    记: Spring Data Jpa @OneToMany 级联查询被动触发的问题
    后端小白的Bootstrap笔记
    最短路径问题
    深度优先搜索 & 广度优先搜索
    检讨书板子
    关于计时器
    博客园美化
    P4819 杀人游戏 (图论 )
    水站 (二分)
    对拍
  • 原文地址:https://www.cnblogs.com/101key/p/15263373.html
Copyright © 2020-2023  润新知