Process request message fields in gRPC Server Interceptor [Java]

333 views
Skip to first unread message

Bill Li

unread,
Jun 17, 2021, 10:36:46 PM6/17/21
to grpc.io
Hi,

I have a question regarding how I can access each field of the input request object in ServerInterceptor. The following is my code:

public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall,
Metadata metadata,
ServerCallHandler<ReqT, RespT> serverCallHandler) {
    ServerCall.Listener<ReqT> listener = serverCallHandler.startCall(serverCall, metadata);
    return new ForwardingServerCallListener.SimpleForwardingServerCallListener<>(listener) {
        @Override
        public void onMessage(ReqT message) {
            System.out.println(message.toString());
            super.onMessage(message);
        }
    };
}

After printing it out, the following output appears:

request_config {
  field_1: "field_1"
  field_2: "field_2"
  field_3: "field_3"
}

I didn't find any obvious methods in the source code or examples online on processing each fields in the message. Does anyone have examples on how to access each field?

Thanks,
Bill

Eric Anderson

unread,
Jun 23, 2021, 10:13:04 AM6/23/21
to Bill Li, grpc.io
Generally the message will be a protobuf message, so you can cast to com.google.protobuf.Message (after doing an instanceof check). You can then use a method like message.getAllFields to introspect. If a field contains a message, you can process the tree recursively.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/bcb78a30-402b-4ea1-bea7-67b3c3912cf5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages