从 spring 引导应用程序连接到 firestore 会引发空指针异常
Connecting to firestore from spring boot application throws a null pointer exception
您好,我正在尝试将 google 云 firestore 连接到 spring 启动应用程序以检索一些数据,但我经常遇到同样的错误。我已经检查了在环境中设置 GOOGLE_CREDENTIALS 变量但没有成功。任何帮助表示赞赏。提前致谢。
服务class代码
public TrainInfo getTrainInfo(String trainId) throws InterruptedException, ExecutionException{
Firestore db=FirestoreOptions.getDefaultInstance().getService();
DocumentReference docRef = db.collection(COLUMN).document(trainId);
ApiFuture<DocumentSnapshot> future = docRef.get();
DocumentSnapshot document = future.get();
TrainInfo trainInfo=null;
if (document.exists()) {
// convert document to POJO
trainInfo = document.toObject(TrainInfo.class);
} else {
System.out.println("No such document!");
}
return trainInfo;
}
显示错误
java.lang.NullPointerException: null
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:892) ~[guava-30.1.1-android.jar:na]
at com.google.firestore.v1.DatabaseRootName.<init>(DatabaseRootName.java:56) ~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
at com.google.firestore.v1.DatabaseRootName.<init>(DatabaseRootName.java:29) ~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
at com.google.firestore.v1.DatabaseRootName$Builder.build(DatabaseRootName.java:157) ~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
at com.google.firestore.v1.DatabaseRootName.of(DatabaseRootName.java:61) ~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
at com.google.cloud.firestore.spi.v1.GrpcFirestoreRpc.<init>(GrpcFirestoreRpc.java:111) ~
从环境中设置凭据对我不起作用,所以我从代码本身进行了设置,如下所示:
凭证凭证 = GoogleCredentials
.fromStream(new FileInputStream("/pathtojsonfile"));
FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(PROJECT_ID).setCredentials(credentials).build();
可能会对某人有所帮助!
您好,我正在尝试将 google 云 firestore 连接到 spring 启动应用程序以检索一些数据,但我经常遇到同样的错误。我已经检查了在环境中设置 GOOGLE_CREDENTIALS 变量但没有成功。任何帮助表示赞赏。提前致谢。
服务class代码
public TrainInfo getTrainInfo(String trainId) throws InterruptedException, ExecutionException{
Firestore db=FirestoreOptions.getDefaultInstance().getService();
DocumentReference docRef = db.collection(COLUMN).document(trainId);
ApiFuture<DocumentSnapshot> future = docRef.get();
DocumentSnapshot document = future.get();
TrainInfo trainInfo=null;
if (document.exists()) {
// convert document to POJO
trainInfo = document.toObject(TrainInfo.class);
} else {
System.out.println("No such document!");
}
return trainInfo;
}
显示错误
java.lang.NullPointerException: null
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:892) ~[guava-30.1.1-android.jar:na]
at com.google.firestore.v1.DatabaseRootName.<init>(DatabaseRootName.java:56) ~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
at com.google.firestore.v1.DatabaseRootName.<init>(DatabaseRootName.java:29) ~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
at com.google.firestore.v1.DatabaseRootName$Builder.build(DatabaseRootName.java:157) ~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
at com.google.firestore.v1.DatabaseRootName.of(DatabaseRootName.java:61) ~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
at com.google.cloud.firestore.spi.v1.GrpcFirestoreRpc.<init>(GrpcFirestoreRpc.java:111) ~
从环境中设置凭据对我不起作用,所以我从代码本身进行了设置,如下所示:
凭证凭证 = GoogleCredentials
.fromStream(new FileInputStream("/pathtojsonfile"));
FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(PROJECT_ID).setCredentials(credentials).build();
可能会对某人有所帮助!