syntax = "proto3";
message User {
string username = 1;
string sex = 2;
int32 age = 3;
Team team = 4;
repeated Team team_list = 5;
message Team {
string ceo = 1;
string cto = 2;
string cfo = 3;
}
}
3.16.0-rc-2
构建对象协议类:
protoc --proto_path=D:\java-basic-with-maven\src\main\java\com\monkey\java_study\proto --java_out=D:\java-basic-with-maven\src\main\java\com\monkey\java_study\proto D:\java-basic-with-maven\src\main\java\com\monkey\java_study\proto\user-from-proto.proto
先使用Builder构建对象,占位,添加属性。
最后输出时,初始化对象:build。
public static byte[] createObjectFromProto() throws InvalidProtocolBufferException {
/**
* 构建对象
* Builder
*/
UserFromProto.User.Builder userBuilder = UserFromProto.User.newBuilder();
/**
* 初始化
* build
*/
userBuilder.setAge(SIX)
.setUsername("xiaoxiao")
.setSex("male");
List<UserFromProto.User.Team> teamList = new ArrayList<>();
/**
* 构建对象,不初始化
* Builder
*/
UserFromProto.User.Team.Builder team = UserFromProto.User.Team.newBuilder();
team.setCeo("ceo");
team.setCfo("cfo");
/**
* 构建对象,不初始化
* Builder
*/
UserFromProto.User.Team.Builder team1 = UserFromProto.User.Team.newBuilder();
team1.setCeo("ceo2");
team1.setCfo("cfo2");
userBuilder.addTeamList(team);
userBuilder.addTeamList(team1);
/**
* 添加列表:初始化对象
*/
teamList.add(team.build());
teamList.add(team1.build());
userBuilder.addAllTeamList(teamList);
logger.info("Proto创建的类:{}", userBuilder);
Gson json = new Gson();
logger.info("gson:{}", json.toJson(userBuilder));
/**
* 初始化对象
* build
*/
return userBuilder.build().toByteArray();
}
使用build初始化对象后,需要为该对象添加属性,使用newBuilder方法,重用当前初始化的对象,添加属性,并初始化:build。
public static byte[] createObjectByBuild() throws InvalidProtocolBufferException {
/**
* 构建对象,不初始化
* Builder
*/
UserFromProto.User.Builder user = UserFromProto.User.newBuilder()
.setAge(100)
.setSex("male");
/**
* 构建对象,并初始化
* build
*/
UserFromProto.User.Team team1 = UserFromProto.User.Team.newBuilder()
.setCeo("ceo1")
.build();
/**
* 使用初始化的对象,继续添加属性,并初始化
* build
*/
team1 = UserFromProto.User.Team.newBuilder(team1).setCfo("cfo1").build();
List<UserFromProto.User.Team> teamList = new ArrayList<>();
teamList.add(team1);
user.addAllTeamList(teamList);
/**
* 初始化对象
*/
logger.info(">>>>>>>>>>User:{}", user.build());
return user.build().toByteArray();
}
package com.monkey.java_study.thirdparty;
import com.google.gson.Gson;
import com.google.protobuf.InvalidProtocolBufferException;
import com.monkey.java_study.common.entity.UserFromProto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import static com.monkey.java_study.common.constant.DigitalConstant.SIX;
/**
* Google proto数据序列化测试.
*
* @author xindaqi
* @date 2021-05-08 9:59
*/
public class GoogleProtoTest {
private static final Logger logger = LogManager.getLogger(GoogleProtoTest.class);
/**
* 通过Google protobuf创建类
*
* @return 对象字节数组
* @throws InvalidProtocolBufferException
*/
public static byte[] createObjectFromProto() throws InvalidProtocolBufferException {
/**
* 构建对象
* Builder
*/
UserFromProto.User.Builder userBuilder = UserFromProto.User.newBuilder();
/**
* 初始化
* build
*/
userBuilder.setAge(SIX)
.setUsername("xiaoxiao")
.setSex("male");
List<UserFromProto.User.Team> teamList = new ArrayList<>();
/**
* 构建对象,不初始化
* Builder
*/
UserFromProto.User.Team.Builder team = UserFromProto.User.Team.newBuilder();
team.setCeo("ceo");
team.setCfo("cfo");
/**
* 构建对象,不初始化
* Builder
*/
UserFromProto.User.Team.Builder team1 = UserFromProto.User.Team.newBuilder();
team1.setCeo("ceo2");
team1.setCfo("cfo2");
userBuilder.addTeamList(team);
userBuilder.addTeamList(team1);
/**
* 添加列表:初始化对象
*/
teamList.add(team.build());
teamList.add(team1.build());
userBuilder.addAllTeamList(teamList);
logger.info("Proto创建的类:{}", userBuilder);
Gson json = new Gson();
logger.info("gson:{}", json.toJson(userBuilder));
/**
* 初始化对象
* build
*/
return userBuilder.build().toByteArray();
}
public static byte[] createObjectByBuild() throws InvalidProtocolBufferException {
/**
* 构建对象,不初始化
* Builder
*/
UserFromProto.User.Builder user = UserFromProto.User.newBuilder()
.setAge(100)
.setSex("male");
/**
* 构建对象,并初始化
* build
*/
UserFromProto.User.Team team1 = UserFromProto.User.Team.newBuilder()
.setCeo("ceo1")
.build();
/**
* 使用初始化的对象,继续添加属性,并初始化
* build
*/
team1 = UserFromProto.User.Team.newBuilder(team1).setCfo("cfo1").build();
List<UserFromProto.User.Team> teamList = new ArrayList<>();
teamList.add(team1);
user.addAllTeamList(teamList);
/**
* 初始化对象
*/
logger.info(">>>>>>>>>>User:{}", user.build());
return user.build().toByteArray();
}
/**
* 解析字节数组,转换为对象
*
* @param params 字节数组
* @throws InvalidProtocolBufferException
*/
public static void parseObjectFromProto(byte[] params) throws InvalidProtocolBufferException {
logger.info("入参:" + params);
UserFromProto.User user = UserFromProto.User.parseFrom(params);
logger.info("Proto解析字节结果:" + user);
logger.info("username: " + user.getUsername());
logger.info("cto:" + user.getTeam().getCto());
List<UserFromProto.User.Team> teamList = user.getTeamListList();
logger.info("team list:" + teamList);
}
public static void main(String[] args) {
try {
byte[] params = createObjectFromProto();
parseObjectFromProto(params);
createObjectByBuild();
} catch(InvalidProtocolBufferException ipe) {
throw new RuntimeException(ipe);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
}