Java .Proto架构文件或字符串中的Proto描述符
我想从定义消息协议的字符串中获取一个proto描述符。例如,我有:Java .Proto架构文件或字符串中的Proto描述符,java,protocol-buffers,proto,Java,Protocol Buffers,Proto,我想从定义消息协议的字符串中获取一个proto描述符。例如,我有: public final static String schema = "" + "message Person {\n" + " required string id = 1;\n" + " required string name = 2;\n" + "}"; @Test public void dynamicProto() throws IOException { Desc
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.proto
文件中有描述,您可以执行:
proto--descriptor\u set\u out=desc.pb schema.proto
稍后,您应该使用以下方式在java中加载此文件:
InputStream input = new FileInputStream("desc.pb");
DescriptorProtos.DescriptorProto desc = DescriptorProtos.DescriptorProto.parseFrom(input);