当我收到带有以下内容的 firestore DocumentSnapshot 字段(即时间戳)时:
DocumentSnapshot snapshot = message.getPayload().getDocumentSnapshot();
Object o = snapshot.get("fieldName);
一切正常,Object o 用真实数据实例化Thu Jan 10 00:00:00 CET 2019
但是当我尝试以 google.cloud.Timestamp 的形式接收该字段时:
DocumentSnapshot snapshot = message.getPayload().getDocumentSnapshot();
Timestamp ts = snapshot.getTimestamp("fieldName");
或者Timestamp ts = (Timestamp) snapshot.get("fieldName"); 它失败并出现错误java.util.Date cannot be cast to com.google.cloud.Timestamp
有人可以澄清这种行为吗?我应该如何从 DocumentSnapshot 访问广告检索 google.cloud.Timestamp 对象?我只对 Timestamp 对象有这个问题,其他所有类型都正常解析。
编辑,添加更多代码:
访问火库:
@Bean
public FirestoreGateway registerFirestoreGateway(FirestoreGatewayProperties properties) throws IOException {
Resource resource = new ClassPathResource(properties.getFirestoreConfiguration());
InputStream configuration = resource.getInputStream();
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(configuration))
.setDatabaseUrl(properties.getDatabaseUrl())
.build();
FirebaseApp.initializeApp(options);
return new FirestoreGateway(FirestoreClient.getFirestore());
}
Firestore 快照侦听器:
@EventListener(ApplicationReadyEvent.class)
public void listenToRequestCommands() {
firestoreConnection.listCollections().forEach(collectionReference -> {
collectionReference
.document(properties.getFirestoreCommand())
.addSnapshotListener((snapshot, e) -> {
Object o = snapshot.get("timestamp");
Timestamp ts = (Timestamp) snapshot.get("timestamp");
}
);
});
}
对象 o通常会解析为正确的值,而同一事件的Timestamp ts会抛出“ java.util.Date cannot be cast to com.google.cloud.Timestamp ”
数据库中的时间戳字段定义:
蝴蝶刀刀
繁星淼淼
相关分类