图文介绍IDEA开发环境安装PROTOBUF插件,生成JAVA代码
https://www.freesion.com/article/55701595432/
(1)按照网址设置部署,部署完成后,点击 Maven--Plugins--protobuf--protobuf:compile-javanano
(2)生成的类在 target\enerate-sources\protobuf下面,将类复制到源码目录,使用即可。
(3)proto文件
syntax = "proto3";
//生成文件所在包名
option java_package = "com.jason.seria.protobuf";
//生成的java文件名
option java_outer_classname = "DemoProto";
message Student {
string name = 1;
int32 age = 2;
}
(4)示例代码
// 创建一个消息对象
DemoProto.Student student1 = new DemoProto.Student();
student1.age = 10;
student1.name = "gggggg";
// 将消息对象序列化为字节数组
byte[] serializedMessage = DemoProto.Student.toByteArray(student1);
// 将字节数组反序列化为消息对象
DemoProto.Student student2 = DemoProto.Student.parseFrom(serializedMessage);