gpt4 book ai didi

java - Spring Boot 可以找到资源文件,但 Java 类失败并出现 NullPointerException

转载 作者:搜寻专家 更新时间:2023-11-01 03:32:36 26 4
gpt4 key购买 nike

我正在做一个项目,在一个类中我需要加载 json 文件 src/main/resources

src/ 
main/java/FirebaseTokenVerifier
main/resources/staging_firebase.json

ForebaseTokenVerifier 类在构造函数中加载此 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-boot:run 时,我看到 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());  

我还确认 json 文件作为

存在于 jar 文件中
✗ 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 中的资源时,您正试图将其作为文件访问。

研究ClassLoader#getResourceAsStream()

关于java - Spring Boot 可以找到资源文件,但 Java 类失败并出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45317304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com