作为java后端,在用protobuf的情况下我该给前端传什么形式的数据?

2025-05-13 02:39:32
推荐回答(1个)
回答1:

protobuf是你们落地时存储的数据格式,跟给前端的格式是没有关系的。
可以通过工具类直接转json后传给前端。
Class clazz = Class.forName(className);
Method method = clazz.getMethod("parseFrom", byte[].class);

JsonFormat().printToString((GeneratedMessage) method.invoke(clazz, bytes));

其中className是你根据protobuf文件生成的java类,bytes是读取的protobuf对象的字节数组。