-->

Protocol message tag had invalid wire type

2020-07-27 03:04发布

问题:

my application sends data through protobuf from a server to a client. When I am deserializing the sent payload on the client side eclipse throws a expection of the follogwing type:

Exception in thread "main" com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.

The expection happens when I call "parseFrom()". I know that in most cases the error lies in a protobuf file with the wrong syntax. Therefore I hope that it is enough to post the protobuf definition here:

   package protobuf;

       option java_package = "com.carproject.abs.demo.protobuf";
       option java_outer_classname = "DesktopDevice_getCarsResponse";

    message CARS {

        required int64 carid = 1;
        required string carname = 2;

        message Carinformation {
            required string street = 1;
            required string postalcode = 2;
            required string city = 3;
            required string country = 4;
            required string cartimezoneid = 5;
        }

        message Right {
            optional string name = 1;
            optional int32 type = 2;
            optional int32 service = 3;
        }

        message PropertyType {
            optional string name = 1;
            optional string value = 2;
        }

        repeated Carinformation carinformation = 3; 
        repeated Right carrights = 4;
        repeated PropertyType carproperties = 5;
        repeated string inoid = 6;
    }

here is the code which shows how the data is written on the server side:

// carObj returns the necessary Strings
CAR carObj = car.getCAR();

Builder newBuilder = DesktopDevice_getCarResponse.CAR.newBuilder();

newBuilder.setCarid( carObj.getCARID() );
newBuilder.setCarname( carObj.getCARNAME());

// hardcoded values here
newBuilder.getCarinformationBuilder(1).setStreet( carObj.getCARNFORMATION().getSTREET() );
newBuilder.getCarinformationBuilder(1).setPostalcode( carObj.getCARINFORMATION().getPOSTALCODE() );
newBuilder.getCarinformationBuilder(1).setCity( carObj.getCARINFORMATION().getCITY() );
newBuilder.getCarinformationBuilder(1).setCountry( fleetObj.getCARINFORMATION().getCOUNTRY() );
newBuilder.getCarinformationBuilder(1).setCartimezoneid( fleetObj.getCARINFORMATION().getCARTIMEZONEID() );

byte[] responsePayload = newBuilder.build().toByteArray();
RestServerResponseMessage responseMsg = new RestServerResponseMessage( requestMsg.getRequestId(), responsePayload, "XML");
return responseMsg;

As you can see, the Server uses the Builder pattern provided by protobuf to set the necessary strings. Then the data is serialized as a byte[] and is sent back to the client via protobuf.

Here is the client code where I try to parse the data.

HttpEntity entity = response.getEntity();

    if (entity != null) {
        InputStream instream = entity.getContent();
        CAR car = DesktopDevice_getCarsResponse.CARS.parseFrom(instream);
        }

The exception is thrown when .parseFrom is called. Server code and carObj are working fine. I already sucessfully sending protobuf data in my program.

回答1:

Use Base64.getEncoder.encode(protoMsg.toByteArray) on server. And use Base64.getDecoder.decode(receivedArray[Bytes]). Data sent on wire should always be encoded and decoded