英文:
FirebaseApp with name [DEFAULT] doesn't exist. Hosted with App Engine. (Spring boot)
问题
Firebase配置类:
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "firebase")
public class FirebaseConfig {
@PostConstruct
public void init() {
try {
FirebaseApp.getInstance();
} catch (IllegalStateException e) {
try {
FileInputStream inputStream = new FileInputStream("json-file-path");
try {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(inputStream)).build();
FirebaseApp.initializeApp(options);
} catch (IOException ioE) {
ioE.printStackTrace();
}
} catch (NullPointerException nullE) {
nullE.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
}
我还尝试使用了类似这样的代码:
public class FirebaseCredentialsHelper {
public String type = "";
public String project_id = "";
public String private_key_id = "";
public String private_key = "";
public String client_email = "";
public String client_id = "";
public String auth_uri = "";
public String token_uri = "";
public String auth_provider_x509_cert_url = "";
public String client_x509_cert_url = "";
}
不幸的是,在构建应用程序时出现错误:“FirebaseApp with name [DEFAULT] doesn't exist.”。在本地环境中正常工作。
附注:已设置为灵活模式。
英文:
Firebase config class :
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "firebase")
public class FirebaseConfig {
@PostConstruct
public void init() {
try {
FirebaseApp.getInstance();
} catch (IllegalStateException e) {
try {
FileInputStream inputStream = new FileInputStream("json-file-path");
try {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(inputStream)).build();
FirebaseApp.initializeApp(options);
} catch (IOException ioE) {
ioE.printStackTrace();
}
} catch (NullPointerException nullE) {
nullE.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
I also tried to use a class like this:
public class FirebaseCredentialsHelper {
public String type= "";
public String project_id= "";
public String private_key_id= "";
public String private_key= "";
public String client_email= "";
public String client_id= "";
public String auth_uri= "";
public String token_uri= "";
public String auth_provider_x509_cert_url= "";
public String client_x509_cert_url= "";
}
Unfortunately when I build the app the error " FirebaseApp with name [DEFAULT] doesn't exist." comes out.
In local works fine.
ps: It's set up flexible
答案1
得分: 0
这可能不适用于Android。等我到那里时会进行更新,如果我到那里的话
为了排除一些可能性:
(“特定于Web应用程序”)
根据他们的文档,并与许多堆栈溢出的响应相反,不需要运行时依赖项“com.google.gms:google-services:4.3.10”(在8.x.x版本中)...
我注意到的一件事是,在为应用程序指定名称并尝试使用FirebaseApp.getInstance(name)之后,它仍然无法识别应用程序是否已被实例化。
另一个注意事项,如果您错误地使用.getApplicationDefault()向应用程序提供Google凭据,Spring应用程序仍然会构建自身。但是,尝试获取服务将会中断,因为Firebase服务本身尚未构建。即使服务在内部崩溃,它显然也有其自己的内置异常处理机制...将凭据sout以确保它们被实例化是一个不错的安全检查。
修复方法:@DependsOn
https://www.baeldung.com/spring-depends-on
注意:您加载凭据的方式并不重要,只要它找得到它们即可
编写一个初始化Firebase并通过IoC启动它的Bean。为Service注解提供“dataSource”名称,并为Bean本身提供一个名称属性。
@Service("dataSource")
class GCP {
@Bean(name = ["Firebase"])
fun initializeFirebase() {
val opts = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(
this::class.java.getResourceAsStream(System.getenv("GCP"))
))
.build()
FirebaseApp.initializeApp(opts)
}
}
现在,无论您在何处使用FirestoreClient.getFirestore()(或其他服务),请确保利用@DependsOn注解来等待并确保Firebase已被实例化。
@Service
@DependsOn(value = ["Firebase"])
class UserService(
private val firestore: Firestore = FirestoreClient.getFirestore()
)
英文:
This may not apply to Android. I'll update this when I get there. if I get there
To rule off some possibilities:
("For Web Apps specifically")
As per their documentation, and contrary to many stack overflow responses, the runtimeOnly dependency "com.google.gms:google-services:4.3.10" is not needed as of version 8.x.x...
One thing I noticed, is after specifying a name for the app, and trying to FirebaseApp.getInstance(name), it was still not recognizing that the app was instantiated.
Another caveat I noticed, if you improperly supply the GoogleCredentials to the application using .getApplicationDefault(), the Spring app will still build itself. But trying to fetch the services will break because the Firebase service itself wasn't built. It apparently has its own built-in exception handling even if the service internally goes kaplooe... It's a good safe check to sout your cred's to make sure they're instantiated.
The Fix: @DependsOn
https://www.baeldung.com/spring-depends-on
NOTE: How you load your credentials doesn't matter, as long as it finds them
Write a bean that initializes Firebase and yeets it with IoC. Provide the "dataSource" name for the Service Annotation, and a name attribute for the Bean itself.
@Service("dataSource")
class GCP {
@Bean(name = ["Firebase"])
fun initializeFirebase() {
val opts = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(
this::class.java.getResourceAsStream(System.getenv("GCP"))
))
.build()
FirebaseApp.initializeApp(opts)
}
}
Now where ever you use FirestoreClient.getFirestore() (Or a diff service), make sure you take advantage of the @DependsOn Annotation to wait and make sure that Firebase was instantiated.
@Service
@DependsOn(value = ["Firebase"])
class UserService(
private val firestore: Firestore = FirestoreClient.getFirestore()
)
专注分享java语言的经验与见解,让所有开发者获益!
评论