我正在做一个项目,其中有一个类需要加载< code>json文件< code>src/main/resources。
src/
main/java/FirebaseTokenVerifier
main/resources/staging_firebase.json
类< code > prebasetokenverifier 将此< code>JSON文件作为
@Component
public class FirebaseTokenVerifier implements TokenVerifier {
private FirebaseAuth firebaseAuth;
// TODO (hhimanshu): resourceFile and DatabaseURL are hardcoded, this should be picked by external file
// or should be picked up conditionally based on environment
public FirebaseTokenVerifier() throws IOException, URISyntaxException {
final String resourceName = "staging_firebase.json";
System.out.println("Getting Resource: " + resourceName);
Path path = Paths.get(ClassLoader.getSystemClassLoader().getResource(resourceName).toURI());
FileInputStream serviceAccount = new FileInputStream(path.toFile());
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl("https://staging-myapp.firebaseio.com")
.build();
FirebaseApp defaultApp = FirebaseApp.initializeApp(options);
firebaseAuth = FirebaseAuth.getInstance(defaultApp);
}
/// more things here .....
}
当我运行 mvn Spring引导:运行时
,我看到 json
文件已正确加载为
2017-07-26 15:57:33.611 INFO 49104 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-07-26 15:57:33.611 INFO 49104 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1424 ms
Getting Resource: staging_firebase.json
2017-07-26 15:57:33.799 INFO 49104 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
一切正常。然后,我试着做以下事情
mvn clean package; java -jar target/myapp-core-0.0.1-SNAPSHOT.jar
但这在运行时失败
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
... 35 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'firebaseTokenVerifier' defined in URL [jar:file:/Users/Harit.Himanshu/bl/sources/idea/myapp-core/targ
et/myapp-core-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/myapp/api/auth/FirebaseTokenVerifier.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationEx
ception: Failed to instantiate [com.myapp.api.auth.FirebaseTokenVerifier]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
... 49 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.myapp.api.auth.FirebaseTokenVerifier]: Constructor threw exception; nested exception is java.lang.NullPointe
rException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
... 61 common frames omitted
Caused by: java.lang.NullPointerException: null
at com.myapp.api.auth.FirebaseTokenVerifier.<init>(FirebaseTokenVerifier.java:31) ~[classes!/:0.0.1-SNAPSHOT]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
... 63 common frames omitted
它在 L31
上失败,即
Path path = Paths.get(ClassLoader.getSystemClassLoader().getResource(resourceName).toURI());
我还确认< code>json文件作为
✗ jar -tvf target/myapp-core-0.0.1-SNAPSHOT.jar| grep json
2355 Wed Jul 26 16:00:44 NZST 2017 BOOT-INF/classes/staging_firebase.json
64952 Tue Jan 07 19:29:24 NZDT 2014 BOOT-INF/lib/json-20140107.jar
这里出了什么问题?为什么Spring-Boot
能够正确解析此路径?
更新
查看后https://stackoverflow.com/a/36372773/379235,我将代码改为
FileInputStream serviceAccount = new FileInputStream(new ClassPathResource(resourceName).getFile());
但它现在没有说
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'firebaseTokenVerifier' defined in URL [jar:file:/Users/Harit.Himanshu/bl/sources/idea/myapp-core/target/myapp-core-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/myapp/api/auth/FirebaseTokenVerifier.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationEx
ception: Failed to instantiate [com.myapp.api.auth.FirebaseTokenVerifier]: Constructor threw exception; nested exception is java.io.FileNotFoundException: class path resource [staging_firebase.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/Harit.Himanshu/bl/sources/idea/myapp-core/target/myapp-core-0.0.1-SNAPSHOT.jar!/BOO
T-INF/classes!/staging_firebase.json
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
... 49 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.myapp.api.auth.FirebaseTokenVerifier]: Constructor threw exception; nested exception is java.io.FileNotFoundException: class path resource [staging_firebase.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/Harit.Himanshu/bl/sources/idea/penn
ytrak-core/target/myapp-core-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/staging_firebase.json
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
... 61 common frames omitted
Caused by: java.io.FileNotFoundException: class path resource [staging_firebase.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/Harit.Himanshu/bl/sources/idea/myapp-core/target/myapp-core-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/staging_firebase.json
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:215) ~[spring-core-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52) ~[spring-core-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at com.myapp.api.auth.FirebaseTokenVerifier.<init>(FirebaseTokenVerifier.java:34) ~[classes!/:0.0.1-SNAPSHOT]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
... 63 common frames omitted
尽管我可以在jar文件中看到这一点
✗ jar -tvf target/myapp-core-0.0.1-SNAPSHOT.jar| grep json
2355 Wed Jul 26 16:54:02 NZST 2017 BOOT-INF/classes/staging_firebase.json
64952 Tue Jan 07 19:29:24 NZDT 2014 BOOT-INF/lib/json-20140107.jar
根据我的经验:
cannot be resolved to absolute file path because it does not reside in the file system
这很明显。当它不是一个文件而是一个嵌入在JAR中的资源时,你试图把它作为文件来访问。
Research ClassLoader#getResourceAsStream()
.
遵循文档中的示例: https://symfony.com/doc/current/page_creation.html 我遇到这个错误消息: 自动加载程序期望类"App\Controller\LuckyController"定义在文件"/var/www/my-project/供应商/作曲家/.../... /src/Controller/LuckyController.php”。找到该文件,但类
我使用Java ASM库编译了一个类(称为Test.class)。我决定尝试使用“java”命令运行该类,以确保已正确编译该类。 然而,当我这样做的时候,我得到了“找不到或加载主类测试”的消息。我觉得这很奇怪,因为类在当前工作目录内(我也尝试过将“.”作为类路径传递,但没有用)。然后我尝试用“javap”反汇编它,这很完美,这很奇怪,因为如果javap能找到类文件,那么java肯定也能找到吗? 这
我们在客户端上安装了我们的根证书,https连接适用于。 但是如果我们尝试使用,它会失败: 证书在客户端上。见: 版本:PIP1.4。1.
问题内容: 我们正在使用加载属性信息。我们的属性文件已经变得非常庞大,我们正在考虑将主属性文件分成几个子模块。有可能实现这一目标吗? 让我知道? 问题答案: 首先,我想知道为什么您选择了。给出问题的表达方式后,您似乎不必关心本地化/国际化或捆绑文件继承。 有了它,它就变得异常容易,因为它实现了反过来又提供了一种合并另一张地图的方法。开球示例: 如果您真的坚持使用,则最好的选择是创建一个自定义,在该
根据Oracle文档 try with resources语句是声明一个或多个资源的try语句。资源是一个对象,在程序完成后必须关闭它。try with resources语句确保在语句末尾关闭每个资源。任何实现java的对象。AutoCloseable,其中包括实现java的所有对象。伊奥。可关闭,可用作资源 如果资源未实现AutoCloseable,则无法在try块中声明,必须在主体部分中声明
我正试图通过Glide将图像加载到。但是图像没有加载-我得到一个错误。我正在使用以下代码 日志