gRPC Java 服务器:有没有办法获取从客户端接收到的原始消息字节?

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

由于 Protobuf 消息序列化格式在平台和软件版本之间不是确定性的,仅使用 toByteArray() 方法将消息序列化回来不一定会产生原始字节。有没有其他方法可以使用 gRPC Java 服务器访问它们?

看起来正确的方法是使用服务器拦截器:

ServerBuilder.forPort(port)
        .addService(
            ServerInterceptors.intercept(
                ServerInterceptors.useInputStreamMessages(
                    new MyMegaCoolService().bindService()
                ),
                new ServerInterceptor() {
                    @Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
                        ServerCall<ReqT,RespT> call,
                        Metadata headers,
                        ServerCallHandler<ReqT,RespT> next
                    ) {
                        ServerCall.Listener<ReqT> listener = next.startCall(call, headers);
                        return new ForwardingServerCallListener
                            .SimpleForwardingServerCallListener<ReqT>(listener) {
                            @Override public void onMessage(ReqT msg) {
                                InputStream in = (InputStream)msg;

                                assert in.markSupported();
                                in.mark(MAX_MESSAGE_SIZE);

                                readMessage(in);

                                try {
                                    in.reset();
                                }
                                catch (IOException e) {
                                    throw new IllegalStateException(e);
                                }

                                super.onMessage(msg);
                            }
                        };
                    }
                }
            )
        )
        .build();