我一直在尝试将Java List>打包为protobuf3消息,而我只是想知道定义一个可以打包此类List的原型文件以及如何有效映射它的最佳方法是什么,因为我已经一直在尝试,无法使其正常工作。
我的最初想法:
// Proto file
message TutorialAPIResponse {
repeated google.protobuf.Any values = 1;
}
//Packaging
List<List<Object>> apiValues = Api.getWhatever();
AnalyzeSignalsResponse response = AnalyzeSignalsResponse.newBuilder()
// line below gives compile error "error: incompatible types: List<List<Object>> cannot be converted to Iterable<? extends Any>"
.addAllValues(values)
.build();
答案 0 :(得分:2)
您真的不能在protobuf中表达一个双重嵌套的列表;您需要表达本身具有列表的东西的列表;所以.. List<Foo>
代表某些Foo
拥有List<Object>
的词条,或以原生泡沫的形式:
message TutorialAPIResponse {
repeated Foo items = 1;
}
message Foo {
repeated google.protobuf.Any values = 1;
}
此外,就我个人而言,我会避免使用Any
。如果您不说Any
就无法表示要返回的内容,那么IMO并不是一个很好的API界面。