is this the correct way of using grpc ?

76 views
Skip to first unread message

omid pourhadi

unread,
Sep 29, 2018, 10:24:27 AM9/29/18
to grpc.io

Hi,

I created a simple crud project based on grpc and spring.I want to know if this is the correct way of using grpc or I'm going to end up failing when my project starts to work ?



Thank you for your time to review it.

I welcome any suggestion.

Carl Mastrangelo

unread,
Oct 1, 2018, 4:44:16 PM10/1/18
to grpc.io
I'm afraid it is not correct.  You should not have to manually parse the protos in your application code.  Also, it seems like it isn't handling errors properly.  It is not correct to log the error, and then continue using the variable later.

Benjamin Krämer

unread,
Oct 2, 2018, 7:57:26 PM10/2/18
to grpc.io
It looks like you wanted to keep the Pagination message independant of your Person message in https://github.com/omidp/grpc-crud/blob/master/grpc-proto/src/main/proto/main.proto

Instead of doing it through bytes, you should prefer the Any type (google.protobuf.Any) which you already import but not use. This will allow you to unpack it as the correct type (PersonFilter) and is internally still stored as bytes. This also works across all languages which makes porting easier. It's never a good idea to do the (de-)serialization manually.

omid pourhadi

unread,
Oct 3, 2018, 8:04:07 AM10/3/18
to grpc.io
Thank you.

I had the same conversation here 


and I came to conclusion that Any is for messages with type and it is not good for java serialization I tried it and I got this error.
Type of the Any message does not match the given class.


Another problem is, since I don't know how to convert bytes (ByteString proto in Java) in message to Java Object I had to manually parse the protos in my application code here.

what is the correct way of doing this ?

Carl Mastrangelo

unread,
Oct 3, 2018, 2:04:08 PM10/3/18
to grpc.io
Responses inline


On Wednesday, October 3, 2018 at 5:04:07 AM UTC-7, omid pourhadi wrote:
Thank you.

I had the same conversation here 


and I came to conclusion that Any is for messages with type and it is not good for java serialization I tried it and I got this error.
Type of the Any message does not match the given class.



You  need to serialize and deserialize with the same Java class in order for it to work.  The Any message contains both the bytes and the message type (as a string) which is how it knows to raise an error.

 
Another problem is, since I don't know how to convert bytes (ByteString proto in Java) in message to Java Object I had to manually parse the protos in my application code here.

what is the correct way of doing this ?

In your proto, request.getSearch().getFilter() is being treated as bytes.  You need to declare that field as the message you want. 

Benjamin Krämer

unread,
Oct 3, 2018, 4:21:59 PM10/3/18
to grpc.io
You can use the Any type with any proto message.Since PersonFilter is now a proto message and not any java class, it should definetly work. You should also check the type of the any before deserializing it if you want to support multiple ones. Not sure how this looks like in Java since I'm mostly working with C#, but as Carl already said, the message type is encoded as string and therefore the dumbest way would be a string comparison to the full name of the message type.

If you only have PersonFilter and don't need the flexibility, you should definetly use PersonFilter as type of the field and not bytes or Any. This makes usability of the API a lot easier and safer.
Reply all
Reply to author
Forward
0 new messages