protobuf使用
Posted linlf03
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了protobuf使用相关的知识,希望对你有一定的参考价值。
一、protobuf环境搭建
Github 地址: https://github.com/protocolbuffers/protobuf
然后进入下载页 https://github.com/protocolbuffers/protobuf/releases
下载系统对应版本编译器
我这里使用的是window 64位
下好之后解压,然后把bin里面的protoc.exe加入到环境变量,
并且把protoc.exe拷贝到C:\\Windows\\System32
二、创建Student.proto 文件
syntax = "proto2"; package com.example.protobuf; option optimize_for = SPEED; option java_package = "com.example.protobuf"; option java_outer_classname = "DataInfo"; message Student required string name = 1; optional int32 age = 2; optional string address = 3;
在工程中增加依赖
<dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.9.1</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java-util</artifactId> <version>3.9.1</version> </dependency>
然后使用protoc命令生成类
D:\\workspace\\study\\basic\\netty_demo>protoc --java_out=src/main/java src/protobuf/Student.proto
最终生成的DataInfo文件
DataInfo.java
// Generated by the protocol buffer compiler. DO NOT EDIT! // source: src/protobuf/Student.proto package com.example.protobuf; public final class DataInfo private DataInfo() public static void registerAllExtensions( com.google.protobuf.ExtensionRegistryLite registry) public static void registerAllExtensions( com.google.protobuf.ExtensionRegistry registry) registerAllExtensions( (com.google.protobuf.ExtensionRegistryLite) registry); public interface StudentOrBuilder extends // @@protoc_insertion_point(interface_extends:com.example.protobuf.Student) com.google.protobuf.MessageOrBuilder /** * <code>required string name = 1;</code> */ boolean hasName(); /** * <code>required string name = 1;</code> */ java.lang.String getName(); /** * <code>required string name = 1;</code> */ com.google.protobuf.ByteString getNameBytes(); /** * <code>optional int32 age = 2;</code> */ boolean hasAge(); /** * <code>optional int32 age = 2;</code> */ int getAge(); /** * <code>optional string address = 3;</code> */ boolean hasAddress(); /** * <code>optional string address = 3;</code> */ java.lang.String getAddress(); /** * <code>optional string address = 3;</code> */ com.google.protobuf.ByteString getAddressBytes(); /** * Protobuf type @code com.example.protobuf.Student */ public static final class Student extends com.google.protobuf.GeneratedMessageV3 implements // @@protoc_insertion_point(message_implements:com.example.protobuf.Student) StudentOrBuilder private static final long serialVersionUID = 0L; // Use Student.newBuilder() to construct. private Student(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) super(builder); private Student() name_ = ""; address_ = ""; @java.lang.Override @SuppressWarnings("unused") protected java.lang.Object newInstance( UnusedPrivateParameter unused) return new Student(); @java.lang.Override public final com.google.protobuf.UnknownFieldSet getUnknownFields() return this.unknownFields; private Student( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException this(); if (extensionRegistry == null) throw new java.lang.NullPointerException(); int mutable_bitField0_ = 0; com.google.protobuf.UnknownFieldSet.Builder unknownFields = com.google.protobuf.UnknownFieldSet.newBuilder(); try boolean done = false; while (!done) int tag = input.readTag(); switch (tag) case 0: done = true; break; case 10: com.google.protobuf.ByteString bs = input.readBytes(); bitField0_ |= 0x00000001; name_ = bs; break; case 16: bitField0_ |= 0x00000002; age_ = input.readInt32(); break; case 26: com.google.protobuf.ByteString bs = input.readBytes(); bitField0_ |= 0x00000004; address_ = bs; break; default: if (!parseUnknownField( input, unknownFields, extensionRegistry, tag)) done = true; break; catch (com.google.protobuf.InvalidProtocolBufferException e) throw e.setUnfinishedMessage(this); catch (java.io.IOException e) throw new com.google.protobuf.InvalidProtocolBufferException( e).setUnfinishedMessage(this); finally this.unknownFields = unknownFields.build(); makeExtensionsImmutable(); public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() return com.example.protobuf.DataInfo.internal_static_com_example_protobuf_Student_descriptor; @java.lang.Override protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internalGetFieldAccessorTable() return com.example.protobuf.DataInfo.internal_static_com_example_protobuf_Student_fieldAccessorTable .ensureFieldAccessorsInitialized( com.example.protobuf.DataInfo.Student.class, com.example.protobuf.DataInfo.Student.Builder.class); private int bitField0_; public static final int NAME_FIELD_NUMBER = 1; private volatile java.lang.Object name_; /** * <code>required string name = 1;</code> */ public boolean hasName() return ((bitField0_ & 0x00000001) != 0); /** * <code>required string name = 1;</code> */ public java.lang.String getName() java.lang.Object ref = name_; if (ref instanceof java.lang.String) return (java.lang.String) ref; else com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; java.lang.String s = bs.toStringUtf8(); if (bs.isValidUtf8()) name_ = s; return s; /** * <code>required string name = 1;</code> */ public com.google.protobuf.ByteString getNameBytes() java.lang.Object ref = name_; if (ref instanceof java.lang.String) com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (java.lang.String) ref); name_ = b; return b; else return (com.google.protobuf.ByteString) ref; public static final int AGE_FIELD_NUMBER = 2; private int age_; /** * <code>optional int32 age = 2;</code> */ public boolean hasAge() return ((bitField0_ & 0x00000002) != 0); /** * <code>optional int32 age = 2;</code> */ public int getAge() return age_; public static final int ADDRESS_FIELD_NUMBER = 3; private volatile java.lang.Object address_; /** * <code>optional string address = 3;</code> */ public boolean hasAddress() return ((bitField0_ & 0x00000004) != 0); /** * <code>optional string address = 3;</code> */ public java.lang.String getAddress() java.lang.Object ref = address_; if (ref instanceof java.lang.String) return (java.lang.String) ref; else com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; java.lang.String s = bs.toStringUtf8(); if (bs.isValidUtf8()) address_ = s; return s; /** * <code>optional string address = 3;</code> */ public com.google.protobuf.ByteString getAddressBytes() java.lang.Object ref = address_; if (ref instanceof java.lang.String) com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (java.lang.String) ref); address_ = b; return b; else return (com.google.protobuf.ByteString) ref; private byte memoizedIsInitialized = -1; @java.lang.Override public final boolean isInitialized() byte isInitialized = memoizedIsInitialized; if (isInitialized == 1) return true; if (isInitialized == 0) return false; if (!hasName()) memoizedIsInitialized = 0; return false; memoizedIsInitialized = 1; return true; @java.lang.Override public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException if (((bitField0_ & 0x00000001) != 0)) com.google.protobuf.GeneratedMessageV3.writeString(output, 1, name_); if (((bitField0_ & 0x00000002) != 0)) output.writeInt32(2, age_); if (((bitField0_ & 0x00000004) != 0)) com.google.protobuf.GeneratedMessageV3.writeString(output, 3, address_); unknownFields.writeTo(output); @java.lang.Override public int getSerializedSize() int size = memoizedSize; if (size != -1) return size; size = 0; if (((bitField0_ & 0x00000001) != 0)) size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, name_); if (((bitField0_ & 0x00000002) != 0)) size += com.google.protobuf.CodedOutputStream .computeInt32Size(2, age_); if (((bitField0_ & 0x00000004) != 0)) size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, address_); size += unknownFields.getSerializedSize(); memoizedSize = size; return size; @java.lang.Override public boolean equals(final java.lang.Object obj) if (obj == this) return true; if (!(obj instanceof com.example.protobuf.DataInfo.Student)) return super.equals(obj); com.example.protobuf.DataInfo.Student other = (com.example.protobuf.DataInfo.Student) obj; if (hasName() != other.hasName()) return false; if (hasName()) if (!getName() .equals(other.getName())) return false; if (hasAge() != other.hasAge()) return false; if (hasAge()) if (getAge() != other.getAge()) return false; if (hasAddress() != other.hasAddress()) return false; if (hasAddress()) if (!getAddress() .equals(other.getAddress())) return false; if (!unknownFields.equals(other.unknownFields)) return false; return true; @java.lang.Override public int hashCode() if (memoizedHashCode != 0) return memoizedHashCode; int hash = 41; hash = (19 * hash) + getDescriptor().hashCode(); if (hasName()) hash = (37 * hash) + NAME_FIELD_NUMBER; hash = (53 * hash) + getName().hashCode(); if (hasAge()) hash = (37 * hash) + AGE_FIELD_NUMBER; hash = (53 * hash) + getAge(); if (hasAddress()) hash = (37 * hash) + ADDRESS_FIELD_NUMBER; hash = (53 * hash) + getAddress().hashCode(); hash = (29 * hash) + unknownFields.hashCode(); memoizedHashCode = hash; return hash; public static com.example.protobuf.DataInfo.Student parseFrom( java.nio.ByteBuffer data) throws com.google.protobuf.InvalidProtocolBufferException return PARSER.parseFrom(data); public static com.example.protobuf.DataInfo.Student parseFrom( java.nio.ByteBuffer data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException return PARSER.parseFrom(data, extensionRegistry); public static com.example.protobuf.DataInfo.Student parseFrom( com.google.protobuf.ByteString data) throws com.google.protobuf.InvalidProtocolBufferException return PARSER.parseFrom(data); public static com.example.protobuf.DataInfo.Student parseFrom( com.google.protobuf.ByteString data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException return PARSER.parseFrom(data, extensionRegistry); public static com.example.protobuf.DataInfo.Student parseFrom(byte[] data) throws com.google.protobuf.InvalidProtocolBufferException return PARSER.parseFrom(data); public static com.example.protobuf.DataInfo.Student parseFrom( byte[] data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException return PARSER.parseFrom(data, extensionRegistry); public static com.example.protobuf.DataInfo.Student parseFrom(java.io.InputStream input) throws java.io.IOException return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input); public static com.example.protobuf.DataInfo.Student parseFrom( java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input, extensionRegistry); public static com.example.protobuf.DataInfo.Student parseDelimitedFrom(java.io.InputStream input) throws java.io.IOException return com.google.protobuf.GeneratedMessageV3 .parseDelimitedWithIOException(PARSER, input); public static com.example.protobuf.DataInfo.Student parseDelimitedFrom( java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException return com.google.protobuf.GeneratedMessageV3 .parseDelimitedWithIOException(PARSER, input, extensionRegistry); public static com.example.protobuf.DataInfo.Student parseFrom( com.google.protobuf.CodedInputStream input) throws java.io.IOException return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input); public static com.example.protobuf.DataInfo.Student parseFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input, extensionRegistry); @java.lang.Override public Builder newBuilderForType() return newBuilder(); public static Builder newBuilder() return DEFAULT_INSTANCE.toBuilder(); public static Builder newBuilder(com.example.protobuf.DataInfo.Student prototype) return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); @java.lang.Override public Builder toBuilder() return this == DEFAULT_INSTANCE ? new Builder() : new Builder().mergeFrom(this); @java.lang.Override protected Builder newBuilderForType( com.google.protobuf.GeneratedMessageV3.BuilderParent parent) Builder builder = new Builder(parent); return builder; /** * Protobuf type @code com.example.protobuf.Student */ public static final class Builder extends com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements // @@protoc_insertion_point(builder_implements:com.example.protobuf.Student) com.example.protobuf.DataInfo.StudentOrBuilder public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() return com.example.protobuf.DataInfo.internal_static_com_example_protobuf_Student_descriptor; @java.lang.Override protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internalGetFieldAccessorTable() return com.example.protobuf.DataInfo.internal_static_com_example_protobuf_Student_fieldAccessorTable .ensureFieldAccessorsInitialized( com.example.protobuf.DataInfo.Student.class, com.example.protobuf.DataInfo.Student.Builder.class); // Construct using com.example.protobuf.DataInfo.Student.newBuilder() private Builder() maybeForceBuilderInitialization(); private Builder( com.google.protobuf.GeneratedMessageV3.BuilderParent parent) super(parent); maybeForceBuilderInitialization(); private void maybeForceBuilderInitialization() if (com.google.protobuf.GeneratedMessageV3 .alwaysUseFieldBuilders) @java.lang.Override public Builder clear() super.clear(); name_ = ""; bitField0_ = (bitField0_ & ~0x00000001); age_ = 0; bitField0_ = (bitField0_ & ~0x00000002); address_ = ""; bitField0_ = (bitField0_ & ~0x00000004); return this; @java.lang.Override public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() return com.example.protobuf.DataInfo.internal_static_com_example_protobuf_Student_descriptor; @java.lang.Override public com.example.protobuf.DataInfo.Student getDefaultInstanceForType() return com.example.protobuf.DataInfo.Student.getDefaultInstance(); @java.lang.Override public com.example.protobuf.DataInfo.Student build() com.example.protobuf.DataInfo.Student result = buildPartial(); if (!result.isInitialized()) throw newUninitializedMessageException(result); return result; @java.lang.Override public com.example.protobuf.DataInfo.Student buildPartial() com.example.protobuf.DataInfo.Student result = new com.example.protobuf.DataInfo.Student(this); int from_bitField0_ = bitField0_; int to_bitField0_ = 0; if (((from_bitField0_ & 0x00000001) != 0)) to_bitField0_ |= 0x00000001; result.name_ = name_; if (((from_bitField0_ & 0x00000002) != 0)) result.age_ = age_; to_bitField0_ |= 0x00000002; if (((from_bitField0_ & 0x00000004) != 0)) to_bitField0_ |= 0x00000004; result.address_ = address_; result.bitField0_ = to_bitField0_; onBuilt(); return result; @java.lang.Override public Builder clone() return super.clone(); @java.lang.Override public Builder setField( com.google.protobuf.Descriptors.FieldDescriptor field, java.lang.Object value) return super.setField(field, value); @java.lang.Override public Builder clearField( com.google.protobuf.Descriptors.FieldDescriptor field) return super.clearField(field); @java.lang.Override public Builder clearOneof( com.google.protobuf.Descriptors.OneofDescriptor oneof) return super.clearOneof(oneof); @java.lang.Override public Builder setRepeatedField( com.google.protobuf.Descriptors.FieldDescriptor field, int index, java.lang.Object value) return super.setRepeatedField(field, index, value); @java.lang.Override public Builder addRepeatedField( com.google.protobuf.Descriptors.FieldDescriptor field, java.lang.Object value) return super.addRepeatedField(field, value); @java.lang.Override public Builder mergeFrom(com.google.protobuf.Message other) if (other instanceof com.example.protobuf.DataInfo.Student) return mergeFrom((com.example.protobuf.DataInfo.Student)other); else super.mergeFrom(other); return this; public Builder mergeFrom(com.example.protobuf.DataInfo.Student other) if (other == com.example.protobuf.DataInfo.Student.getDefaultInstance()) return this; if (other.hasName()) bitField0_ |= 0x00000001; name_ = other.name_; onChanged(); if (other.hasAge()) setAge(other.getAge()); if (other.hasAddress()) bitField0_ |= 0x00000004; address_ = other.address_; onChanged(); this.mergeUnknownFields(other.unknownFields); onChanged(); return this; @java.lang.Override public final boolean isInitialized() if (!hasName()) return false; return true; @java.lang.Override public Builder mergeFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException com.example.protobuf.DataInfo.Student parsedMessage = null; try parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); catch (com.google.protobuf.InvalidProtocolBufferException e) parsedMessage = (com.example.protobuf.DataInfo.Student) e.getUnfinishedMessage(); throw e.unwrapIOException(); finally if (parsedMessage != null) mergeFrom(parsedMessage); return this; private int bitField0_; private java.lang.Object name_ = ""; /** * <code>required string name = 1;</code> */ public boolean hasName() return ((bitField0_ & 0x00000001) != 0); /** * <code>required string name = 1;</code> */ public java.lang.String getName() java.lang.Object ref = name_; if (!(ref instanceof java.lang.String)) com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; java.lang.String s = bs.toStringUtf8(); if (bs.isValidUtf8()) name_ = s; return s; else return (java.lang.String) ref; /** * <code>required string name = 1;</code> */ public com.google.protobuf.ByteString getNameBytes() java.lang.Object ref = name_; if (ref instanceof String) com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (java.lang.String) ref); name_ = b; return b; else return (com.google.protobuf.ByteString) ref; /** * <code>required string name = 1;</code> */ public Builder setName( java.lang.String value) if (value == null) throw new NullPointerException(); bitField0_ |= 0x00000001; name_ = value; onChanged(); return this; /** * <code>required string name = 1;</code> */ public Builder clearName() bitField0_ = (bitField0_ & ~0x00000001); name_ = getDefaultInstance().getName(); onChanged(); return this; /** * <code>required string name = 1;</code> */ public Builder setNameBytes( com.google.protobuf.ByteString value) if (value == null) throw new NullPointerException(); bitField0_ |= 0x00000001; name_ = value; onChanged(); return this; private int age_ ; /** * <code>optional int32 age = 2;</code> */ public boolean hasAge() return ((bitField0_ & 0x00000002) != 0); /** * <code>optional int32 age = 2;</code> */ public int getAge() return age_; /** * <code>optional int32 age = 2;</code> */ public Builder setAge(int value) bitField0_ |= 0x00000002; age_ = value; onChanged(); return this; /** * <code>optional int32 age = 2;</code> */ public Builder clearAge() bitField0_ = (bitField0_ & ~0x00000002); age_ = 0; onChanged(); return this; private java.lang.Object address_ = ""; /** * <code>optional string address = 3;</code> */ public boolean hasAddress() return ((bitField0_ & 0x00000004) != 0); /** * <code>optional string address = 3;</code> */ public java.lang.String getAddress() java.lang.Object ref = address_; if (!(ref instanceof java.lang.String)) com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; java.lang.String s = bs.toStringUtf8(); if (bs.isValidUtf8()) address_ = s; return s; else return (java.lang.String) ref; /** * <code>optional string address = 3;</code> */ public com.google.protobuf.ByteString getAddressBytes() java.lang.Object ref = address_; if (ref instanceof String) com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (java.lang.String) ref); address_ = b; return b; else return (com.google.protobuf.ByteString) ref; /** * <code>optional string address = 3;</code> */ public Builder setAddress( java.lang.String value) if (value == null) throw new NullPointerException(); bitField0_ |= 0x00000004; address_ = value; onChanged(); return this; /** * <code>optional string address = 3;</code> */ public Builder clearAddress() bitField0_ = (bitField0_ & ~0x00000004); address_ = getDefaultInstance().getAddress(); onChanged(); return this; /** * <code>optional string address = 3;</code> */ public Builder setAddressBytes( com.google.protobuf.ByteString value) if (value == null) throw new NullPointerException(); bitField0_ |= 0x00000004; address_ = value; onChanged(); return this; @java.lang.Override public final Builder setUnknownFields( final com.google.protobuf.UnknownFieldSet unknownFields) return super.setUnknownFields(unknownFields); @java.lang.Override public final Builder mergeUnknownFields( final com.google.protobuf.UnknownFieldSet unknownFields) return super.mergeUnknownFields(unknownFields); // @@protoc_insertion_point(builder_scope:com.example.protobuf.Student) // @@protoc_insertion_point(class_scope:com.example.protobuf.Student) private static final com.example.protobuf.DataInfo.Student DEFAULT_INSTANCE; static DEFAULT_INSTANCE = new com.example.protobuf.DataInfo.Student(); public static com.example.protobuf.DataInfo.Student getDefaultInstance() return DEFAULT_INSTANCE; @java.lang.Deprecated public static final com.google.protobuf.Parser<Student> PARSER = new com.google.protobuf.AbstractParser<Student>() @java.lang.Override public Student parsePartialFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException return new Student(input, extensionRegistry); ; public static com.google.protobuf.Parser<Student> parser() return PARSER; @java.lang.Override public com.google.protobuf.Parser<Student> getParserForType() return PARSER; @java.lang.Override public com.example.protobuf.DataInfo.Student getDefaultInstanceForType() return DEFAULT_INSTANCE; private static final com.google.protobuf.Descriptors.Descriptor internal_static_com_example_protobuf_Student_descriptor; private static final com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internal_static_com_example_protobuf_Student_fieldAccessorTable; public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() return descriptor; private static com.google.protobuf.Descriptors.FileDescriptor descriptor; static java.lang.String[] descriptorData = "\\n\\032src/protobuf/Student.proto\\022\\024com.exampl" + "e.protobuf\\"5\\n\\007Student\\022\\014\\n\\004name\\030\\001 \\002(\\t\\022\\013\\n\\003a" + "ge\\030\\002 \\001(\\005\\022\\017\\n\\007address\\030\\003 \\001(\\tB\\"\\n\\024com.example" + ".protobufB\\010DataInfoH\\001" ; descriptor = com.google.protobuf.Descriptors.FileDescriptor .internalBuildGeneratedFileFrom(descriptorData, new com.google.protobuf.Descriptors.FileDescriptor[] ); internal_static_com_example_protobuf_Student_descriptor = getDescriptor().getMessageTypes().get(0); internal_static_com_example_protobuf_Student_fieldAccessorTable = new com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( internal_static_com_example_protobuf_Student_descriptor, new java.lang.String[] "Name", "Age", "Address", ); // @@protoc_insertion_point(outer_class_scope)
三、测试
public class ProtoBufTest public static void main(String[] args) throws Exception DataInfo.Student student = DataInfo.Student.newBuilder(). setName("张三").setAge(30).setAddress("ZheJiang").build(); //将对象转byte数组 byte[] student2ByteArray = student.toByteArray(); //将byte数组转对象 DataInfo.Student student2 = DataInfo.Student.parseFrom(student2ByteArray); System.out.println(student2.getName()); System.out.println(student2.getAddress()); System.out.println(student2.getAge());
输出结果:
以上是关于protobuf使用的主要内容,如果未能解决你的问题,请参考以下文章