java - 将 Protobuf 转换为 Java
问题描述
我知道可以将 protobuf 消息转换为 Java 类。我想知道是否可以在不编写翻译函数的情况下将 protobuf 消息转换为 Java 对象?
解决方案
是的,这是可能的。使用 protobuf 转换器。检查:https ://github.com/BAData 。
将域对象转换为 Protobuf:
ProtoObject protoObject =
Converter.create().toProtobuf(ProtoObject.class, domainObject);
将 Protobuf 对象转换为域对象:
DomainObject domainObject = Converter.create().toDomain(DomainObject.class, protoObject)
您的域类需要使用@ProtoClass 进行注释,指定您希望域类映射到的原型类。例子:
@ProtoClass(ProtoObject.class)
Class DomainClass{
@ProtoField
private String field1;
@ProtoField(name = "xyz") // in case proto and domain class field have different names
private String field2;
}
我一直在使用它,它非常易于使用并且节省了很多精力。
推荐阅读
- c# - Unity:ScreenCapture.CaptureScreenShot() 在 VR 上将图像倒置旋转,我不知道为什么
- python-3.x - 从另一个类访问单个变量 (Python)
- javascript - 替换正则表达式匹配的字符串的字符
- java - 弹簧靴 | 多调度器 Servlet
- python - 如何在 INSERT .. ON DUPLICATE KEY UPDATE 后从 sqlalchemy 获取 lastrowid?
- editor - Geany 不打印几个字符组合
- azure - Kusto:make-series 在第一天停止 - 没有按预期工作
- java - 在 mac 上运行 example.app 文件时,通过 java 中的错误 - 异常 java.lang.ClassNotFoundException: org.sqlite.JDBC
- text-to-speech - 生成 mp3 文本转语音文件的 cURL 命令请求是什么?
- php - Jquery:不通过 Ajax 从 php 页面接收数据