.proto
文件定义
message StudentRequest {
string name = 1;
}
生成Java代码
protoc --java_out=. java/com/zjw/grpc/greeter.proto
Java代码测试
package com.zjw.grpc;
import com.zjw.grpc.gen.StudentRequest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/** * Created by zjwblog<co.zjwblog@gmail.com> on 2021/7/20 */
public class ProtoBufReadWriteTest {
public static void main(String[] args) throws Exception {
StudentRequest tom = StudentRequest.newBuilder().setName("Tom").build();
tom.writeTo(new FileOutputStream("/tmp/tmp.out"));
StudentRequest student = StudentRequest.parseFrom(new FileInputStream("/tmp/tmp.out"));
System.out.println(student.getName());
}
}
还没有评论,来说两句吧...