我在 Quarkus 中有一个基于 gRPC 的项目。我必须返回从 PostgreSQL 数据库获取的一些数据,其中一些列是
timestamp without time zone
类型 — 映射到 Java 中的 LocalDateTime
。
create table users (
id uuid not null default gen_random_uuid(),
...
created_on timestamp without time zone not null default localtimestamp(6),
modified_on timestamp without time zone not null default localtimestamp(6),
primary key (id)
);
syntax = "proto3";
import "google/type/datetime.proto"; <- Doesn't work :/
...
message GetUserResponse {
optional string id = 1;
...
optional google.type.DateTime created_on = 5;
optional google.type.DateTime modified_on = 6;
}
public class User {
private UUID id;
...
private LocalDateTime createdOn;
private LocalDateTime modifiedOn;
}
我尝试将
com.google.api.grpc:proto-google-common-protos:2.33.0
导入到项目中,以便我可以使用 google/type/datetime.proto
,但不知何故,这些类型无法被识别。
$ ./gradlew clean quarkusBuild
> Task :clean
> Task :processResources
google/type/datetime.proto: File not found.
user.proto:12:1: Import "google/type/datetime.proto" was not found or had errors.
user.proto:43:12: "google.type.DateTime" is not defined.
user.proto:44:12: "google.type.DateTime" is not defined.
注意:我当然可以在Java中使用
,因为我看到OffsetDateTime
被识别。google/protobuf/timestamp.proto
和OffsetDateTime
都可以让我存储我想要的内容,因为我全面使用UTC,但我宁愿解决问题并避免更改太多可能会破坏其他内容的内容。LocalDateTime
有没有办法注册来自
com.google.api.grpc:proto-google-common-protos
的类型?我的假设是,只需导入该库,它们就应该可用。
有一个用于 gRPC 代码生成的 Quarkus guide。我认为这可能会解决这个问题,但到目前为止,使用
quarkus.generate-code.grpc.scan-for-imports
、quarkus.generate-code.grpc.scan-for-proto
或 quarkus.generate-code.grpc.scan-for-proto-include.
没有任何区别。
gRPC 代码生成参考指南记录了根据项目中可用的内容扫描/包含/生成代码的不同方法。
就我而言,由于依赖项中已经有
com.google.api.grpc:proto-google-common-protos
,我只想使项目中的所有 *.proto
文件可用,所以我使用了 scan-for-imports
:
quarkus:
generate-code:
grpc:
scan-for-imports: all
还有一种方法可以按组和工件“仅扫描”指定的依赖项。这样好多了,但是语法有点尴尬。