使用 Protobuf 生成 Java 文件
首先需要安装 protobuf 编译器,可以从 protobuf 官网下载对应系统的安装包进行安装。
然后,需要编写.proto 文件,定义所需的消息类型和服务类型。例如:
syntax = 'proto3';
package com.example;
message Person { string name = 1; int32 age = 2; }
service PersonService { rpc GetPerson(PersonRequest) returns (PersonResponse) {} }
message PersonRequest { string name = 1; }
message PersonResponse { Person person = 1; bool success = 2; string message = 3; }
接着,使用 protobuf 编译器将 .proto 文件编译成 java 文件。可以使用以下命令:
protoc --java_out=./src/main/java ./path/to/your/proto/file.proto
其中,--java_out 表示将生成的 java 文件输出到指定目录下,./path/to/your/proto/file.proto 表示 .proto 文件的路径。
执行完该命令后,即可在指定目录下看到生成的 java 文件。
原文地址: https://www.cveoy.top/t/topic/m81K 著作权归作者所有。请勿转载和采集!