簡體   English   中英

如何在另一個“消息”中使用“消息”對協議緩沖區對象進行解碼?

[英]How can I decode a Protocol Buffer object with a “message” within another “message”?

我在使用Ruby編碼協議緩沖區對象並在Java中對其解碼時遇到問題。

一方面,我有以下.proto文件:

package foo;

message Message {   
    message Stats {
        required string session = 1;
        required string client = 2;
        required string providerCode = 3;
        required string startTime = 4;
        required string endTime = 5;
        required string execTime = 6;
        required string serviceApi = 7;
        required string travelOperation = 8;
        required string serviceOperation = 9;
        required string errorCode = 10;
        required string providerHubStatus = 11;
    }

    required Stats stats = 1;
}

當我在Ruby中記錄原型對象的內容(使用.inspect函數)時,其值為:

#<Foo::Message stats=#<Foo::Message::Stats session="1888ddb0-4371-55af-92d2-a63436fa5509" client="log" providerCode="EMP" startTime="2016-03-09 15:06:36" endTime="2016-03-09 15:06:40" execTime="3873" serviceApi="hot" travelOperation="avail" serviceOperation="Disponibilidad" errorCode="\\x30" providerHubStatus="OK">>

該消息似乎是正確的。

當我嘗試用Java解碼消息時出現問題,發生以下錯誤:

com.google.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This could mean either than the input has been truncated or that an embedded message misreported its own length.

另一方面,我對以下.proto文件進行了測試:

package foo;

message Stats {
    required string session = 1;
    required string client = 2;
    required string providerCode = 3;
    required string startTime = 4;
    required string endTime = 5;
    required string execTime = 6;
    required string serviceApi = 7;
    required string travelOperation = 8;
    required string serviceOperation = 9;
    required string errorCode = 10;
    required string providerHubStatus = 11;
}

當我記錄原型對象的內容(message.inspect)時,該值為:

#<Foo::Stats session="1888ddb0-4371-55af-92d2-a63436fa5509" client="log" providerCode="EMP" startTime="2016-03-09 15:06:36" endTime="2016-03-09 15:06:40" execTime="3873" serviceApi="hot" travelOperation="avail" serviceOperation="Disponibilidad" errorCode="\\x30" providerHubStatus="OK">

在這種情況下,該消息似乎又是正確的,現在,解碼進行得很好。


為什么如果我有一條唯一的消息“ Stats”,則解碼進行得很好,而當我在另一條消息中有一條消息時,解碼就出錯了?

我應該為第一種情況做些特別的事情嗎?

根據我的經驗,我可以告訴您,Java方面沒有什么特別的。

我在protoc表現良好的一個項目的pom.xml中具有以下依賴項:

    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>2.5.0</version>
    </dependency>

調試提示: protoc有一個命令行選項--decode可以測試二進制消息。 要將問題固定到一側,您應該嘗試以此反序列化您的消息。

編輯

在驗證了生產方(紅寶石)的結果之后,行為異常的潛在來源減少到:

  • 網絡通訊
  • Java方面
    • 錯誤的類/類版本(請確保您重新生成了反序列化器代碼)
    • protoc錯誤( 不可能,但可能)

為了一個接一個地消除,我假設您嘗試從磁盤反序列化已經用protoc --decode ...測試過的格式良好的軟件包。

注意:這些是二進制數據。 您不得將其作為字符串處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM