简体   繁体   English

protobufjs 编码一个包装消息,其中包含一个类型为 google.protobuf.Any 的内部字段

[英]protobufjs encode a wrapper message which includes an inner field with type google.protobuf.Any

I am using protobufjs library in Nodejs to send a message to a kafka topic and read the message from a java consumer.我在 Nodejs 中使用 protobufjs 库向 kafka 主题发送消息并从 java 消费者读取消息。

Following are the protobuf definition.以下是 protobuf 定义。 The Inner message should be packed in the Wrapper message field. Inner 消息应该打包在 Wrapper 消息字段中。

message Wrapper {
  google.protobuf.Any message = 1;
}

message Inner {
  string field1 = 1;
}

I used the following three methods to encode the message but when deserializing i get error "Type of the Any message does not match the given class."我使用以下三种方法对消息进行编码,但在反序列化时出现错误“任何消息的类型与给定的 class 不匹配。” in the java consumer.在 java 消费者中。

const rootWrapper = await protobuf.load('wrapper.proto');
const Wrapper = rootWrapper.lookupType('Wrapper');
const rootInner = await protobuf.load('inner.proto');
const Inner = rootInner.lookupType('Inner');
const rootAnyProto = await protobuf.load('node_modules/google-proto-files/google/protobuf/any.proto');
const Any = rootAnyProto.lookupType('google.protobuf.Any');

Method 1方法一

const innerMsg = { field1 : 'value1'}
const wrapper = { message: innerMsg }

const encodedWrapper = Wrapper.encode(wrapper).finish()

Method 2方法二

const innerMsg = { field1 : 'value1'}
const innerMsgEncoded = Inner.encode(innerMsg).finish()
const wrapper = { message: innerMsgEncoded }

const encodedWrapper = Wrapper.encode(wrapper).finish()

Method 3方法三

const innerMsg = { field1 : 'value1'}
const innerMsgEncoded = Any.encode(innerMsg).finish()
const wrapper = { message: innerMsgEncoded }

const encodedWrapper = Wrapper.encode(wrapper).finish()

Anyone can help on the correct way to encode an protobuf message with an inner message?任何人都可以帮助以正确的方式使用内部消息对 protobuf 消息进行编码?

After many tries i resolved my issue in the following manner.经过多次尝试,我通过以下方式解决了我的问题。

const innerMsg = { field1 : 'value1'}
const innerMsgEncoded = Any.encode(innerMsg).finish()
const any = Any.create({ type_url : 'type.googleapis.com/Inner', value : innerMsgEncoded });

const wrapper = { message: any}
const encodedWrapper = Wrapper.encode(wrapper).finish()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 gRPC nodejs 客户端中解压 google.protobuf.Any 类型? - How to unpack an google.protobuf.Any type in gRPC nodejs client? 在 nodejs 上使用 protobufjs 使用来自 .net 的 protobuf 消息时出现无效的线路类型和索引超出范围错误 - Invalid wire type and index out of range errors when consuming a protobuf message from .net with protobufjs on nodejs protobufjs:编码/解码具有Any组件的消息 - protobufjs : encoding/decoding a message that has an Any component Firebase:如何修复错误:ENOENT:没有这样的文件或目录,打开“node_modules/protobufjs/google/protobuf/api.proto” - Firebase: how to fix Error: ENOENT: no such file or directory, open 'node_modules/protobufjs/google/protobuf/api.proto' 错误:字段Message.Field .protobuf.MessageTypeAck.sourceModuleID:1(预期为0)的非法线路类型 - Error: Illegal wire type for field Message.Field .protobuf.MessageTypeAck.sourceModuleID: 1 (0 expected) 如何检测消息是否包含任何用户提及? - How to detect if message includes any user mention? 有什么方法可以按标签解码/编码,但在 protobuf.js 中没有任何 .proto 文件或 json 文件? - Is there any way to decode/encode by tag but without any .proto file or json file in protobuf.js? Protobuf 编码返回 null 值 - Protobuf encode returns null values Node.js protobuf 包括 - Node.js protobuf includes 无法使用 protobuf.js 加载 protobuf 消息 - Cant load protobuf message with protobuf.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM