简体   繁体   中英

How to pass in json as payload in .proto

As per the following page I should be able to send in json payload : https://developers.google.com/protocol-buffers/docs/proto3 under 'JSON Mapping'.

I would like to send in json payload as part of the message and I have the following .proto file :

message EventsRequest{
    message RequestElement {
        struct payload = 1;
    }
    string customerId = 1;
    repeated RequestElement jsonPayload = 2;
}


message EventsResponse {
    int32 status = 1;
    string rawResponseData = 2;
    struct responseData = 3;
}

But compiling it gives me the following error :

[INFO] Compiling 1 proto file(s) to C:\workspace\...\target\generated-sources\protobuf\java
[ERROR] PROTOC FAILED: msg_service.proto:21:9: "struct" is not defined.
msg_service.proto:34:5: "struct" is not defined.

[ERROR] C:\workspace\...\src\main\proto\msg_service.proto [0:0]: msg_service.proto:21:9: "struct" is not defined.
msg_service.proto:34:5: "struct" is not defined.

I have tried 'Struct' also, but I got the same error.

Am I misunderstanding usage ? If I have to send in json payload, do I pass in as a string ?

Thanks

我最终使用String表示json负载。

If you would like to use a Struct you will need to first import:

import "google/protobuf/struct.proto";

Then during the declaration instead of just the word Struct use google.protobuf.Struct

It should be like this,

syntax = "proto3";

package db;
import "google/protobuf/struct.proto";

service Proxy
{
    rpc appConfig(UserId) returns (AppConfig);
}

message UserId
{
    string userId= 1;
}

message AppConfig
{
    Struct appConfig = 1;
}


应该是Struct ,大写S

I think the issue is it is not able to import the right message from google/protobuf/struct.proto , so I used

google.protobuf.Struct field_name = 1

and it worked for me !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM