[英]Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor
I tried FileNotFoundException and try, catch but did not help.我尝试了FileNotFoundException 并尝试捕获但没有帮助。 I think problem is here我认为问题就在这里
InputStream serviceAccount = new FileInputStream("/src/main/resources/static/FirebaseAdminSDKJava.json");
Full class source code:全类源代码:
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";
}
}
I am using Spring framework.我正在使用 Spring 框架。
FirebaseAdminSDKJava.json located: src -> main -> resources -> static -> FirebaseAdminSDKJava.json FirebaseAdminSDKJava.json位于:src -> main -> resources -> static -> FirebaseAdminSDKJava.json
"/src/main/resources/static/FirebaseAdminSDKJava.json" is a relative file path. “/src/main/resources/static/FirebaseAdminSDKJava.json”是一个相对文件路径。 Depending on where your application is running, this file may or may not be in that position.根据您的应用程序运行的位置,此文件可能位于也可能不在该位置。 Once built and deployed, the "src" dir won't be there, only in development.构建和部署后,“src”目录将不存在,仅在开发中。
Instead, load this file from the classpath.相反,从类路径加载此文件。 See this answer: How to really read text file from classpath in Java请参阅此答案: How to true read text file from classpath in Java
Classpath includes what you have inside you resources directory.类路径包括您在资源目录中的内容。 please try this请试试这个
1. InputStream in = this.getClass().getClassLoader()
.getResourceAsStream("FirebaseAdminSDKJava.json");
2 classpath:static/FirebaseAdminSDKJava.json
For debugging, 1. start eclipse in debugger mode or put your code in try block and instead of catching FileNotFoundException catch parent exception "Exception".对于调试, 1. 在调试器模式下启动 eclipse 或将您的代码放在 try 块中,而不是捕获 FileNotFoundException 捕获父异常“异常”。
sample ex样品前
try{
//your code
}catch(Exception ex){
ex.printStackTrace();
}
printStackTrace() will show us the actual root cause of the issue printStackTrace() 将向我们展示问题的实际根本原因
hope it will work for you希望它对你有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.