下面列出了怎么用io.grpc.protobuf.lite.ProtoLiteUtils的API类实例代码及写法,或者点击链接到github查看源代码。
public void greet() throws IOException {
GreeterOuterClass.Request request = GreeterOuterClass.Request
.newBuilder()
.setName("aaa")
.build();
try {
GreeterOuterClass.Response response = this.blockingStub.hello(request);
System.out.println(response.getMsg());
} catch (StatusRuntimeException e){
System.out.println(e.toString());
System.out.println(e.getStatus().getCode());
Metadata metadata = e.getTrailers();
com.google.rpc.Status status = metadata.get(Metadata.Key.of("grpc-status-details-bin",
ProtoLiteUtils.metadataMarshaller(com.google.rpc.Status.getDefaultInstance())));
if(status.getDetailsCount() > 0){
Any any = status.getDetails(0);
GreeterOuterClass.Request req = any.unpack(GreeterOuterClass.Request.class);
System.out.println(req.getName());
}
}
}
/**
* Sets the global registry for proto marshalling shared across all servers and clients.
*
* <p>Warning: This API will likely change over time. It is not possible to have separate
* registries per Process, Server, Channel, Service, or Method. This is intentional until there
* is a more appropriate API to set them.
*
* <p>Warning: Do NOT modify the extension registry after setting it. It is thread safe to call
* {@link #setExtensionRegistry}, but not to modify the underlying object.
*
* <p>If you need custom parsing behavior for protos, you will need to make your own
* {@code MethodDescriptor.Marshaller} for the time being.
*
* @since 1.16.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1787")
public static void setExtensionRegistry(ExtensionRegistry registry) {
ProtoLiteUtils.setExtensionRegistry(registry);
}
/**
* Create a {@link Marshaller} for protos of the same type as {@code defaultInstance}.
*
* @since 1.0.0
*/
public static <T extends Message> Marshaller<T> marshaller(final T defaultInstance) {
return ProtoLiteUtils.marshaller(defaultInstance);
}
/**
* Produce a metadata marshaller for a protobuf type.
*
* @since 1.13.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4477")
public static <T extends Message> Metadata.BinaryMarshaller<T> metadataMarshaller(T instance) {
return ProtoLiteUtils.metadataMarshaller(instance);
}
/**
* Sets the global registry for proto marshalling shared across all servers and clients.
*
* <p>Warning: This API will likely change over time. It is not possible to have separate
* registries per Process, Server, Channel, Service, or Method. This is intentional until there
* is a more appropriate API to set them.
*
* <p>Warning: Do NOT modify the extension registry after setting it. It is thread safe to call
* {@link #setExtensionRegistry}, but not to modify the underlying object.
*
* <p>If you need custom parsing behavior for protos, you will need to make your own
* {@code MethodDescriptor.Marshaller} for the time being.
*
* @since 1.16.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1787")
public static void setExtensionRegistry(ExtensionRegistry registry) {
ProtoLiteUtils.setExtensionRegistry(registry);
}
/**
* Create a {@link Marshaller} for protos of the same type as {@code defaultInstance}.
*
* @since 1.0.0
*/
public static <T extends Message> Marshaller<T> marshaller(final T defaultInstance) {
return ProtoLiteUtils.marshaller(defaultInstance);
}
/**
* Produce a metadata marshaller for a protobuf type.
*
* @since 1.13.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4477")
public static <T extends Message> Metadata.BinaryMarshaller<T> metadataMarshaller(T instance) {
return ProtoLiteUtils.metadataMarshaller(instance);
}