错误信息:
While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either that the input has been truncated or that an embedded message misreported its own length.
出现这个错误有很多种,
我遇到这个错误的原因:
消息中间件是rabbitmq, 消息的生产端发送的消息并不是toByteArray 处理后到rabbitmq的,而是通过JsonFormat.printer().print() 处理过的json格式的字符串发送到rabbitmq。 消费端如果是直接parseFrom byte[] 格式的数据就会报上面的错误。
如何解决:
通过
body 为byte[] 类型
String json = new String(body); // 转字符串
Message builder = TestProto.Msg.newBuilder();
JsonFormat.parser().merge(json, builder);
TestProto.Msg msg = (TestProto.Msg) builder.build();