protobuf java object_protobuf 与 Java 对象互转
Java使用protobuf时,往往需要在Java对象(比如POJO)和protobuf message的Java对象间互转:
protobuf接口需要传入message参数: 把POJO转成message, 作为接口参数;
protobuf接口会返回message参数: 需要把message转成POJO, 供后续使用.
比如:
protobuf的message
message HelloRequest {
string name = 1;
int32 type = 2;
string location = 3;
bool flag = 4;
}
Java的POJO:
@Getter
@Setter
@ToString
public class HelloDTO {
private String name;
private Integer type;
private String location;
private Boolean flag;
private String message;
}
POJO -> Message
Hello.HelloRequest.Builder builder = Hello.HelloRequest.newBuilder();
builder.setFlag(true);
builder.setName("Name1");
builder.setLocation("Location1");
builder.setType(1);
Hel
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
