【问题标题】:Spring Boot "FirebaseApp with name [DEFAULT] doesn't exist."Spring Boot“名称为 [DEFAULT] 的 FirebaseApp 不存在。”
【发布时间】:2021-10-17 23:49:07
【问题描述】:

启动 Spring Boot's jar file 会引发以下错误:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'temperatureController' defined in URL <...>

Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'temperatureService' defined in URL <...>

Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.temperaturetracker.services.TokenService]: Constructor threw exception; nested exception is java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.

每个类都包含适当的@@Service@RestController@SpringBootApplication@Entity@Repository

一些类:

@Service
public class TemperatureService {

    private final AlertService alertService;

    @Autowired
    public TemperatureService(AlertService alertService) {
        this.alertService = alertService;
    }

    <...>
}

@Service
class AlertService @Autowired constructor(private val tokenService: TokenService,
                                          private val cloudMessagingService: CloudMessagingService) {
  
    @PostConstruct
    fun initialize() {
        <...>
    }
}

@Service
public class CloudMessagingService {

    final Logger logger = LoggerFactory.getLogger(CloudMessagingService.class);

    public void sendFirebaseMessage() {
        <...>

        try {
            var response = FirebaseMessaging.getInstance().send(fbMessage);
            logger.debug("Notification response: " + response);
        } catch (FirebaseMessagingException e) {
            e.printStackTrace();
            logger.error("Error sending Firebase Cloud Message: " + e);
        }
    }

}

@Service
public class FirebaseInitialize {

    @PostConstruct
    public void initialize() {
        try {
            FileInputStream serviceAccount =
                    new FileInputStream("hidden-path");

            FirebaseOptions options = FirebaseOptions.builder()
                    .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                    .setDatabaseUrl("hidden-path")
                    .build();

            FirebaseApp.initializeApp(options);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

@SpringBootApplication
public class TemperatureTrackerApplication {

    public static void main(String[] args) {
        SpringApplication.run(TemperatureTrackerApplication.class, args);
    }

}

这些错误仅在我启动 jar 文件时发生。通过绿色箭头或 Shift + F10 运行应用程序一切正常。

【问题讨论】:

    标签: java firebase spring-boot firebase-cloud-messaging


    【解决方案1】:

    我已将班级的 FirebaseInitialize 方法 initialize() 更改为:

    try {
        ClassPathResource serviceAccount =
                new ClassPathResource("myFile.json"); // it is in resources folder
    
        FirebaseOptions options = FirebaseOptions.builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount.getInputStream()))
                .setDatabaseUrl("database-path-provided-by-firebase.app")
                .build();
    
        FirebaseApp.initializeApp(options);
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    FileInputStream 我之前使用过预期资源位于文件系统上,它不能嵌套在jar 文件中。因此,使用getInputStream() 中的ClassPathResource 是可行的。

    请阅读更多:Classpath resource not found when running as jar

    【讨论】:

      【解决方案2】:

      确保您的 Firebase 配置正常,因为 SpringBoot 尝试执行该类时会引发错误

      Firebase 初始化

      【讨论】:

      • 嗨,我应该检查什么或如何检查?因为一切都是通过正常运行而不是jar来工作的?
      • 嗨,不,抱歉之前我没有使用 Firebase:/
      猜你喜欢
      • 2016-09-17
      • 2019-12-04
      • 2016-12-07
      • 2017-04-25
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      相关资源
      最近更新 更多