有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring Boot“名为[DEFAULT]的FirebaseApp不存在。”

启动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运行应用程序,一切都能完美运行


共 (2) 个答案

  1. # 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. # 2 楼答案

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

    FirebaseInitialize