grpc 多种返回类型

问题描述 投票:0回答:2

让更多人获得服务回报的最佳方法是什么?我的意思是,我希望单个方法能够返回多个类型,在这种情况下,find 方法将返回用户或响应

syntax = "proto3";

service UserService {
    rpc Add(User) returns (Response);
    rpc Find(Id) returns (User);
}

message Response {
    string message = 1;
}

message Id {
    string id = 1;
}

message User {
    string id = 1;
    int32 money = 2;
}
  
protocol-buffers proto
2个回答
1
投票

您可以返回一个包含响应和用户的对象。

    syntax = "proto3";

    service UserService {
        rpc Add(User) returns (Response);
        rpc Find(Id) returns (FindResponse);
    }
    
    message Response {
        string message = 1;
    }
    
    message Id {
        string id = 1;
    }
    
    message User {
        string id = 1;
        int32 money = 2;
    }

    message FindResponse{
        Response response = 1;
        User user = 2;
    }

0
投票

我知道有一个选定的答案,但根据问题,更好的选择可能是这样的:

message FindResponse
{
    oneof fresponse
    {
        Response response = 1;
        User user = 2;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.