proto3中的RPC语法允许空请求或响应吗?
例如,我想要以下内容的等价物:
rpc Logout;
rpc Status returns (Status);
rpc Log (LogData);或者我应该只创建一个null类型?
message Null {};
rpc Logout (Null) returns (Null);
rpc Status (Null) returns (Status);
rpc Log (LogData) returns (Null);发布于 2015-08-02 22:39:12
Kenton下面的评论是合理的建议:
...作为开发人员,我们真的不擅长猜测我们未来可能想要什么。因此,我建议安全起见,总是为每个方法定义自定义参数和结果类型,即使它们是空的。
回答我自己的问题:
查看默认的proto文件时,我发现Empty与我上面建议的Null类型完全相同:)
摘录自该文件:
// A generic empty message that you can re-use to avoid defining duplicated
// empty messages in your APIs. A typical example is to use it as the request
// or the response type of an API method. For instance:
//
// service Foo {
// rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty);
// }
//
message Empty {
}发布于 2019-10-22 20:45:35
您还可以使用预定义的:
import "google/protobuf/empty.proto";
package MyPackage;
service MyService {
rpc Check(google.protobuf.Empty) returns (google.protobuf.Empty) {}
}发布于 2019-10-08 15:36:11
您还可以在回复结构中使用另一个bool属性。像这样
message Reply {
string result = 1;
bool found = 2;
}因此,如果您找不到结果或发生了某些错误,您可以从服务类中返回以下内容
return new Reply()
{
Found = false
};https://stackoverflow.com/questions/31768665
复制相似问题