Protobuffer生成java类

protobuffer文件


package tutorial; option java_package = "com.example.tutorial"; option java_outer_classname = "AddressBookProtos"; message Person { required string name = 1; required int32 id = 2; optional string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phone = 4; } message AddressBook { repeated Person person = 1; }


windows环境里下载protoc.exe
https://developers.google.com/protocol-buffers/docs/downloads


在PersonMsg.proto的文件夹内执行命令:
protoc.exe --java_out=E:\java PersonMsg.proto


如果一个proto文件太大,生成的类代码太多,一个java文件存不下,可以在proto文件里添加
option java_multiple_files = true;

这样,一个proto文件会生成多个java文件



相关链接:
https://github.com/google/protobuf/releases?after=v3.0.0-alpha-4.1
https://github.com/google/protobuf/tree/master/java


posted @ 2020-03-08 11:44  牧云文仔  阅读(1683)  评论(0编辑  收藏  举报