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

夜晚降临,又是程序员最精神的时刻,项目中用到了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

你可能感兴趣的:(Java,spring,mvc,Spring,protobuf)