I have a simple Spring Boot application with connection to the Cloud Firestore on Firebase. I can send some requests to this app, then Spring retrieves some documents from Firestore and returns to me in appropriate format. Recently I have deployed this application to the Mogenius (alternative for Heroku) as a docker container. I have also uploaded service account key generated and downloaded from Firebase. Everything is working fine locally with the same json file but on deployed container I'm facing the following issue:
java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. Available app names: appName
I had this issue locally some time ago (but if I remember correctly, there was only first sentence, without "Available app names:...") and I have solved it by inserting app initializing to the following if statement (solution-for-app-name-collision):
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options, "appName");
} else {
FirebaseApp.initializeApp(options);
}
So finally, my code with Firebase integration is:
ClassLoader classLoader = CookingRecipesBackendApplication.class.getClassLoader();
URL credentialsUrl = classLoader.getResource("serviceAccountKey.json");
String urlForFile = Optional.ofNullable(credentialsUrl)
.map(URL::getFile)
.orElse("/mo-data/serviceAccountKey.json");
File file = new File(urlForFile);
FileInputStream serviceAccount = new FileInputStream(file.getAbsolutePath());
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://url-to-database.com")
.build();
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options, "appName");
} else {
FirebaseApp.initializeApp(options);
}
And this code is located in main method of SpringBootApplication class, before calling run(). I have tried a lot of solutions like:
- Fixing initialization App by calling this method with "appName" (intuitive solution from error).
- Adding com.google.gms:google-services dependency (google-dependency-solution)
- Generating new private key from Firebase and use different one on deployed app and locally.
Moreover, I think that it is not caused of not finding service account key. When I put this json file in incorrect directory, Spring Boot didn't wake up.
I'm newbie with Firebase integration and deploying application to dedicated servers, so could you help me with solving it?