java - Java Maven 项目 - Firebase 连接错误:java.lang.NoSuchMethodError
问题描述
java.lang.NoSuchMethodError: com.google.common.collect.Iterables.getFirst(Ljava/lang/Iterable;Ljava/lang/Object;)Ljava/lang/Object; 在 com.google.auth.oauth2.OAuth2Credentials.getFromServiceLoader(OAuth2Credentials.java:334) ...
Java测试用例:
public void connectToFirebase() throws IOException {
System.out.println("connectToFirebase()");
FileInputStream serviceAccount;
serviceAccount = new FileInputStream
("C:\\...\\social-networking-site-ae94d-firebase-adminsdk-xxxxx-xxxxxxxxxx.json");
Map<String, Object> auth = new HashMap<String, Object>();
auth.put("uid", "my-service-worker");
System.out.println("-1-"); // error occurred after this line
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://social-networking-site-ae94d.firebaseio.com")
.setDatabaseAuthVariableOverride(auth)
.build();
FirebaseApp.initializeApp(options);
System.out.println("-2-");
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference("/some_resource");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
String res = (String) dataSnapshot.getValue();
System.out.println(res);
}
public void onCancelled(DatabaseError error) {
}
});
}
pom.xml:
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>6.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.common/google-collect -->
<dependency>
<groupId>com.google.common</groupId>
<artifactId>google-collect</artifactId>
<version>0.5</version>
</dependency>
...
解决方案
推荐阅读
- wordpress - 如果在 WooCommerce 中访问结帐,则将未登录的用户重定向到我的帐户
- java - 即使在 Eclipse 中安装 javafx 后,我也无法运行 javafx 程序
- python-3.x - Project Euler 10 的慢速编译
- python - 如何将 Numba 添加到代码中以加快进程?
- javascript - 为什么下面的代码在 JavaScript 中有效并且没有给出编译器错误
- sql - 将子关系映射到父关系的复杂递归调用(SQL/Pyspark)
- php - 如何运行 sass
- postgresql - AWS:如何将 rds 引擎类型从 Aurora 降级为 Postgres?
- node.js - 通过 Cloud Build 部署到 App Engine 时出错
- api - 如何针对 JSON 响应的子对象而不是 REST ASSURED 中的整个对象验证模式