ProtoBuf与Java结合异常记录--- Protocol message end-group tag did not match expected tag

7 篇文章 0 订阅
3 篇文章 0 订阅

夜晚降临,又是程序员最精神的时刻再见再见,项目中用到了google protocol buffer也就是大佬们简称的protobuf。

简单的解释如下(摘自百度百科,其实最好的话还是去官网去看看介绍):

protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台。google 提供了多种语言的实现:java、c#c++、go 和 python,每一种实现都包含了相应语言的编译器以及库文件。由于它是一种二进制的格式,比使用 xml 进行数据交换快许多。可以把它用于分布式应用之间的数据通信或者异构环境下的数据交换。作为一种效率和兼容性都很优秀的二进制数据传输格式,可以用于诸如网络传输、配置文件、数据存储等诸多领域。

当然既然提到了去官网瞅瞅,俺也从官网摘了一段介绍供各位看客一瞅为先:

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the "old" format.

大体意思和百度百科类似都是介绍了protobuf的特性,下面是官网网址,国内最近有点慢:

https://developers.google.com/protocol-buffers/docs/overview

回归正题,异常记录:

1.Proto_Balance.Balance.parseFrom(参数)该方法中直接将byteouStream.toByteArray()的byte数组传入之后也会出现上面的错误,转而再通过一层流之后将流传入之后就正常运转了。

                         

         temp = iterator.next();
	Object obj = temp.get("data");
	ByteArrayOutputStream byteouStream = new ByteArrayOutputStream();
	ObjectOutputStream out = new ObjectOutputStream(byteouStream);
	out.writeObject(obj);
        ####################出错后添加的一层流
        ByteArrayInputStream in = new ByteArrayInputStream(byteouStream.toByteArray());
	ObjectInputStream input = new ObjectInputStream(in);
        ####################
	Balance balance = Proto_Balance.Balance.parseFrom(input);
	ret_list.add(balance);
2.Spring mvc的controller返回的protobuf的实体类,spring无法识别,出现如下异常:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

本来是打算将结果作为json数据传输到前台的,失败之后才发现spring并不能识别proto编译产生的java实体类的,这就比较尴尬喽,所以是不能直接将其返回的,具体其他的后续操作就不提了,不同的系统有不同的应对方式吧安静


                                                                                                                       -----以此谨记当年的那些bug

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值