gRPC:如何在Java客户端与Go服务器中获取多个返回值

huangapple go评论108阅读模式
英文:

gRPC: How to get multiple return values in Java client with Go server

问题

我有一个protocol buffer文件:

syntax = "proto3";
package v1api;
option java_multiple_files = true;
option java_package = "myApp.v1";
option java_outer_classname = "V1";

service API {
    rpc Login(LoginRequest) returns (LoginResponse)
}

message LoginRequest {
    int32 pin = 1;
}

message LoginResponse {
    string token = 1;
}

我用Go语言编写了服务器(可以返回多个值),客户端是一个Android应用程序。

当我使用这个protoBuf生成服务器的Go代码时,大致如下:

...
func (c *aPIClient) Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error) {
    out := new(LoginResponse)
    err := grpc.Invoke(ctx, "/v1api.API/Login", in, out, c.cc, opts...)
    if err != nil {
        return nil, err
    }
    return out, nil
}
...

请注意,有两个返回值,(*LoginResponse, error)

现在,当我使用这个protoBuf生成我的Android端的Java代码时,大致如下:

public myApp.v1.LoginResponse login(myApp.v1.LoginRequest request) {
    return blockingUnaryCall(getChannel(), METHOD_LOGIN, getCallOptions(), request);
}

请注意,在这里,只有一个返回值,就像Java允许的那样,myApp.v1.LoginResponse

我的问题是,如果服务器返回错误(例如:return nil, err),我该如何在Android端获取第二个返回值。

英文:

I have a protocol buffer file:

syntax = "proto3";
package v1api;
option java_multiple_files = true;
option java_package = "myApp.v1";
option java_outer_classname = "V1";

service API {
    rpc Login(LoginRequest) returns (LoginResponse)
}

message LoginRequest {
    int pin = 1
}

message LoginResponse {
    string token = 1
}

I have my server written in Go (a language that can return multiple values), and my client being an Android application.

When I use this protoBuf to generate Go code for server, it is something like:

...
func (c *aPIClient) Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error) {
    out := new(LoginResponse)
    err := grpc.Invoke(ctx, "/v1api.API/Login", in, out, c.cc, opts...)
    if err != nil {
	    return nil, err
    }
    return out, nil
}
...

Notice that there are two return values, (*LoginResponse, error).

Now, when I use this protoBuf to generate Java code for my Android side, I get something like:

public myApp.v1.LoginResponse login(myApp.v1.LoginRequest request) {
    return blockingUnaryCall(getChannel(), METHOD_LOGIN, getCallOptions(), request);
}

Notice that, here, there is just one return value like Java allows, myApp.v1.LoginResponse.

My question is, in case of an error returned by the server (something like: return nil, err), how will I get that second return value in my Android side.

答案1

得分: 2

在Go语言中,你的Login方法返回的错误将会被转换成gRPC使用的线路格式状态codemessage。在你的情况下,如果你返回一个status.Status,那么Code和message将会被发送并显示在Java端作为一个StatusRuntimeException。如果你返回一个非Status错误,gRPC将会将其转换为UNKNOWN代码,但保留message。

这就是为什么Go语言有两个返回值,而Java只有一个返回值的原因。Java可以通过抛出异常来“返回”一个次要值。

英文:

In Go, the error returned by your Login method will get converted into the wire format status code and message used by gRPC. In your case, if you return a status.Status, the Code and message will be sent and show up on the Java side as a StatusRuntimeException. If you return a non Status error, gRPC will convert it to the UNKNOWN code, but preserve the message.

This is the reason there are two return values for Go, vs. just one for Java. Java can "return" a secondary value by throwing an exception in its place.

答案2

得分: 0

由于Java不支持多个返回值,当一个protoBuf生成为Java代码时,错误通过抛出异常来进行通信。在try-catch块中进行gRPC调用,并捕获调用可能抛出的任何StatusRuntimeException异常。

英文:

Since Java does not support multiple return values, when a protoBuf is generated into a java code, the errors are communicated by throwing exceptions. Make your gRPC call in a try-catch block and catch any StatusRuntimeException that the call may throw.

huangapple
  • 本文由 发表于 2017年5月22日 20:08:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/44112495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定