Boot默认应用上的Firebase

Boot默认应用上的Firebase

本文介绍了Spring Boot默认应用上的Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个JHipster应用程序(Springboot + Angular),并且需要集成Firebase.

I am creating a JHipster application (Springboot + Angular) and I need to integrate Firebase.

我已经按照Firebase文档上的说明进行操作,并且当我离线启动服务器时,它可以很好地工作.

I've followed the instructions on firebase documentation and it works perfectly when I start the sever offline.

但是当我在控制台中使用jhipster aws将服务器部署到AWS时,当我尝试调用使用Firebase SDK的函数时,它会显示Trying to login to firebase failed. Reason: FirebaseApp name [DEFAULT] already exists!

But when I deploy the server to AWS using jhipster aws in console, when I try to call a function that uses the firebase SDK it says Trying to login to firebase failed. Reason: FirebaseApp name [DEFAULT] already exists!

仅当我部署应用程序时才会发生此行为,因为如果从本地主机调用它,它将运行完美.

This behaviour happens only when I deploy the application because if I call it from a localhost it works perfectly.

我在pom.xml上有这个

I have this on my pom.xml

<dependency>
        <groupId>com.google.firebase</groupId>
        <artifactId>firebase-admin</artifactId>
        <version>6.3.0</version>
    </dependency>

    <dependency>
        <groupId>com.google.gms</groupId>
        <artifactId>google-services</artifactId>
        <version>3.1.1</version>
    </dependency>

我正在使用来初始化我的应用

and I'm initializing my app using

try{
        InputStream serviceAccount = new ByteArrayInputStream(getFirebaseJson().getBytes(StandardCharsets.UTF_8));
        //FileInputStream serviceAccount = new FileInputStream("firebaseAuth.json");

        FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .setDatabaseUrl("https://seeu-soon.firebaseio.com/")
            .build();



        FirebaseApp.initializeApp(options);
        FirebaseDatabase.getInstance(FirebaseApp.getInstance()).setPersistenceEnabled(true);

    }catch(Exception e){
        log.debug("Trying to login to firebase failed. Reason: " + e.getMessage());
    }

推荐答案

我已修复它.

事实证明,将环境部署到AWS Elastic Beanstalk时,FirebaseApp.initializeApp(options);函数在api上执行一次.

Turns out that the FirebaseApp.initializeApp(options); function was executed once on the api when the environment was deployed to the AWS Elastic Beanstalk.

我所做的是创建一个名为FirebaseUtils的类,该类具有一个initiateFirebase()过程,该过程设置了FirebaseApp.

What I did was to create a class called FirebaseUtils which has a initiateFirebase() procedure, which sets the FirebaseApp.

在每次需要访问Firebase的API调用上调用initiateFirebase()之后,就会使用新的选项配置创建FirebaseApp,该配置位于另一个作用域中,并且我可以访问Firebase.

Once I call the initiateFirebase() on each call of the API where I need access to firebase, the FirebaseApp is created with a new options configuration which is in another scope and I can access Firebase.

这篇关于Spring Boot默认应用上的Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:28