[英]Import proto files from classpath (JAR) to build another proto file
[英]Proto Descriptor from .proto schema file or String
我想從定義消息協議的字符串中獲取原型描述符。 例如我有:
public final static String schema = ""
+ "message Person {\n"
+ " required string id = 1;\n"
+ " required string name = 2;\n"
+ "}";
@Test
public void dynamicProto() throws IOException {
DescriptorProtos.DescriptorProto descriptor = DescriptorProtos.DescriptorProto.parseFrom(schema.getBytes());
boolean test = true;
//do stuff
}
我收到以下異常:com.google.protobuf.InvalidProtocolBufferException:協議消息標簽的線路類型無效。
最終,我希望能夠定義一個模式並在運行時接受實際的 proto 消息,而不是在編譯時接受一些模擬服務類型的東西。
嘗試創建schema
字符串的描述符格式文件。 如果您的消息在schema.proto
文件中描述,您可以執行:
protoc --descriptor_set_out=desc.pb schema.proto
稍后,您應該使用以下命令在 java 中加載此文件:
InputStream input = new FileInputStream("desc.pb");
DescriptorProtos.DescriptorProto desc = DescriptorProtos.DescriptorProto.parseFrom(input);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.