【问题标题】:gRPC generated file doesn't have json definitiongRPC 生成的文件没有 json 定义
【发布时间】:2022-01-21 13:34:40
【问题描述】:

我正在尝试从 proto 文件生成 Go 文件,但它在方法的输入定义中没有 json 定义。我应该自己添加 json 定义还是我的脚本有问题。谢谢你,我真诚地感谢你的帮助。

原始文件

message RateRequest {
    string Base = 1;
    string Destination = 2;
}

生成的文件

type RateRequest struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    //No json definition here
    Base        string `protobuf:"bytes,1,opt,name=Base,proto3" json:"Base,omitempty"`
    Destination string `protobuf:"bytes,2,opt,name=Destination,proto3" json:"Destination,omitempty"`
}

协议脚本

protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
protos/currency.proto

grpcurl

grpcurl --plaintext -d '{Base: "GBP", Destination: "USD"}' localhost:9092 Currency.GetRate
// Error invoking method "Currency.GetRate": error getting request data: message type RateRequest has no known field named base

【问题讨论】:

    标签: go microservices grpc protoc


    【解决方案1】:

    因为错误是

    获取请求数据时出错:消息类型 RateRequest 没有名为 base 的已知字段

    json

    json:"Base,omitempty"`
    

    然后好像是找错了字段,应该是

    json:"base,omitempty"`
    

    【讨论】:

    • 谢谢,我用grpcurl --plaintext -d '{"Base":"GBP", "Destination":"USD"}' localhost:9092 Currency.GetRate 运行了 grpcurl,效果很好!
    • 太棒了,很高兴它成功了:)
    猜你喜欢
    • 2013-09-29
    • 2017-02-04
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 2018-07-23
    • 2015-10-27
    相关资源
    最近更新 更多