繁体   English   中英

默认构造函数无法处理隐式超级构造函数抛出的异常类型 FileNotFoundException。 必须定义一个显式构造函数

[英]Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor

我尝试了FileNotFoundException 并尝试捕获但没有帮助。 我认为问题就在这里

InputStream serviceAccount = new FileInputStream("/src/main/resources/static/FirebaseAdminSDKJava.json");

全类源代码:

package uz.xose.webapp;

import java.io.FileInputStream;
import java.io.InputStream;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.Firestore;

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.cloud.FirestoreClient;



@RestController
public class HelloController { 

    InputStream serviceAccount = new FileInputStream("/src/main/resources/static/FirebaseAdminSDKJava.json");

    GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);

    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(credentials)
        .build();

    FirebaseApp.initializeApp(options);

    Firestore db = FirestoreClient.getFirestore();

     @RequestMapping("/")
     public String index() {
         return "This is the index!\n";
     }

     @RequestMapping("/hello")
     public String index2() {

         return "Hello, World!\n";
     }
}

我正在使用 Spring 框架。

FirebaseAdminSDKJava.json位于:src -> main -> resources -> static -> FirebaseAdminSDKJava.json

“/src/main/resources/static/FirebaseAdminSDKJava.json”是一个相对文件路径。 根据您的应用程序运行的位置,此文件可能位于也可能不在该位置。 构建和部署后,“src”目录将不存在,仅在开发中。

相反,从类路径加载此文件。 请参阅此答案: How to true read text file from classpath in Java

类路径包括您在资源目录中的内容。 请试试这个

1. InputStream in = this.getClass().getClassLoader()
                                .getResourceAsStream("FirebaseAdminSDKJava.json");


2 classpath:static/FirebaseAdminSDKJava.json

对于调试, 1. 在调试器模式下启动 eclipse 或将您的代码放在 try 块中,而不是捕获 FileNotFoundException 捕获父异常“异常”。

样品前

try{
   //your code
}catch(Exception ex){
  ex.printStackTrace();
}

printStackTrace() 将向我们展示问题的实际根本原因

希望它对你有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM