标签:cat stat roo pac option http mamicode build sys
gradle依赖库:
implementation ‘com.google.protobuf:protobuf-java:3.4.0‘ implementation ‘com.google.protobuf:protobuf-java-util:3.4.0‘
0.编写.proto文件,编译生成对应Java源文件:
syntax = "proto2"; option java_generic_services = true; option java_package = "gj.protobuf.sample"; option java_outer_classname = "ProtoSample"; message Student { required int32 id = 1; optional string name = 2; }
protoc --java_out=..\..\java ProtobufSample.proto
1.编写Java文件:
package gj.protobuf.sample; import com.google.protobuf.InvalidProtocolBufferException; /** * Author: areful */ public class ProtoSampleTest { public static void main(String[] args) { ProtoSample.Student student = ProtoSample.Student.newBuilder() .setId(1) .setName("areful") .build(); System.out.println(student); byte[] data = student.toByteArray(); try { ProtoSample.Student student1 = ProtoSample.Student.parseFrom(data); System.out.println(student1); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } } }
2.编译运行:
标签:cat stat roo pac option http mamicode build sys
原文地址:https://www.cnblogs.com/areful/p/10410333.html