- Protocol Buffers 教程
- Protocol Buffers - 首页
- Protocol Buffers - 简介
- Protocol Buffers - 基本应用
- Protocol Buffers - 结构
- Protocol Buffers - 消息
- Protocol Buffers - 字符串
- Protocol Buffers - 数字
- Protocol Buffers - 布尔值
- Protocol Buffers - 枚举
- Protocol Buffers - 重复
- Protocol Buffers - 映射
- Protocol Buffers - 嵌套类
- Protocol Buffers - 可选性和默认值
- Protocol Buffers - 语言无关性
- Protocol Buffers - 复合数据类型
- Protocol Buffers - 命令行使用
- Protocol Buffers - 更新定义的规则
- Protocol Buffers - 与 Kafka 集成
- Protocol Buffers - 在其他语言中
- Protocol Buffers 有用资源
- Protocol Buffers - 快速指南
- Protocol Buffers - 有用资源
- Protocol Buffers - 讨论
Protocol Buffers - 嵌套类
概述
在这里,我们将了解如何创建嵌套类。Protobuf 将其转换为嵌套的Java类。
继续我们从Protocol Buffers - 字符串章节开始的剧院示例,以下是我们需要使用的语法,以指示 Protobuf 我们将创建一个重复的 -
theater.proto
syntax = "proto3"; package theater; option java_package = "com.tutorialspoint.theater"; message Theater { TheaterOwner owner = 10; } message TheaterOwner{ string name = 1; string address = 2; }
现在我们的消息类包含一个嵌套类,即关于剧院所有者信息。
从 Proto 文件创建 Java 类
要使用 Protobuf,我们现在必须使用protoc二进制文件从这个“.proto”文件创建所需的类。让我们看看如何做到这一点 -
protoc --java_out=. theater.proto
这将在当前目录的com > tutorialspoint > theater文件夹中创建一个TheaterOuterClass.java类。我们在应用程序中使用此类,类似于Protocol Buffers - 基本应用章节中所做的那样。
使用从 Proto 文件创建的 Java 类
TheaterWriter.java
package com.tutorialspoint.theater; import java.io.FileOutputStream; import java.io.IOException; import com.tutorialspoint.theater.TheaterOuterClass.Theater; import com.tutorialspoint.theater.TheaterOuterClass.TheaterOwner; public class TheaterWriter{ public static void main(String[] args) throws IOException { TheaterOwner owner = TheaterOwner.newBuilder() .setName("Anthony Gonsalves") .setAddress("513, St Paul Street, West Coast, California") .build(); Theater theater = Theater.newBuilder() .setOwner(owner) .build(); String filename = "theater_protobuf_output"; System.out.println("Saving theater information to file: " + filename); try(FileOutputStream output = new FileOutputStream(filename)){ theater.writeTo(output); } System.out.println("Saved theater information with following data to disk: \n" + theater); } }
接下来,我们将有一个读取器来读取剧院信息 -
TheaterReader.java
package com.tutorialspoint.theater; import java.io.FileInputStream; import java.io.IOException; import com.tutorialspoint.theater.TheaterOuterClass.Theater; import com.tutorialspoint.theater.TheaterOuterClass.Theater.Builder; public class TheaterReader{ public static void main(String[] args) throws IOException { Builder theaterBuilder = Theater.newBuilder(); String filename = "theater_protobuf_output"; System.out.println("Reading from file " + filename); try(FileInputStream input = new FileInputStream(filename)) { Theater theater = theaterBuilder.mergeFrom(input).build(); System.out.println(theater); } } }
编译项目
现在我们已经设置了读取器和写入器,让我们编译项目。
mvn clean install
序列化 Java 对象
现在,编译后,让我们先执行写入器 -
> java -cp .\target\protobuf-tutorial-1.0.jar com.tutorialspoint.theater.TheaterWriter Saving theater information to file: theater_protobuf_output Saved theater information with following data to disk: owner { name: "Anthony Gonsalves" address: "513, St Paul Street, West Coast, California" }
反序列化序列化对象
现在,让我们执行读取器以从同一文件读取 -
java -cp .\target\protobuf-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader Reading from file theater_protobuf_output owner { name: "Anthony Gonsalves" address: "513, St Paul Street, West Coast, California" }
因此,正如我们所看到的,我们能够通过将二进制数据反序列化为剧院对象来读取序列化的嵌套类。
广告