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

Abdul Wasae :

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.

Carl Mastrangelo :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Interface Python Thrift client with Go gRPC server

How to use Prometheus to monitor grpc-java server and client?

How to get client certificates in Go HTTPS server

How to get client ip from request metadata with grpc-java

How to set up gRPC client - server correctly

How to shutdown gRPC server from gRPC client in python?

Java How to handle multiple return Values?

gRPC Java server: is there any way to get the original message bytes that were received from a client?

go grpc server doesn't known client ungraceful dead

How do I force my gRPC client to open multiple connections to the server?

return only a single of multiple return values in Go

Go server and Java client communication

How to create Insecure Java grpc cilent for secure go grpc service

How to implement Grpc username/password authentication. Python client, Java server

gRPC client in C# does not work with gRPC server in Go with mTLS support

How to handle multiple ruby services in gRPC server

How to return multiple values

Java: How to go about return values in a try-catch?

How to assert gRPC error codes client side in Go

How to confirm gRPC traffic from Go client is TLS encrypted

How does gRPC client-streaming flow control work in go?

how to implement non-blocking client in grpc go?

gRPC, Go & Cloud Run - How to elegantly handle client authorisation?

Grpc Server keeps processing the data even when client get disconnected

Error connecting Node.js web client to Java gRPC server

Perform client authentication to server with gRPC in Java with only a CA

JAVA Grpc Client

Go based grpc server stream keeps stacking up the response to go client

Python: How to get multiple return values from a threaded function

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive