spring-boot - 无法在 Spring Boot 应用程序中打开 ServletContext 资源
问题描述
我创建了 Spring Boot 应用程序,它将连接到 Google Cloud pub/sub 并从主题中获取数据。对于身份验证,我已spring.cloud.gcp.credentials.location=C:\\Users\\Admin\\dev\\Projects\\cloud\\credentials-file.json
在application.properties
文件中添加了此属性。
当我运行应用程序时,我得到了这个异常
> Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/C:/Users/Admin/dev/Projects/cloud/credentials-file.json]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:158) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.cloud.gcp.core.DefaultCredentialsProvider.<init>(DefaultCredentialsProvider.java:94) ~[spring-cloud-gcp-core-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration.googleCredentials(GcpContextAutoConfiguration.java:58) ~[spring-cloud-gcp-autoconfigure-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration$$EnhancerBySpringCGLIB$$375b81d4.CGLIB$googleCredentials$1(<generated>) ~[spring-cloud-gcp-autoconfigure-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration$$EnhancerBySpringCGLIB$$375b81d4$$FastClassBySpringCGLIB$$81fbe7e5.invoke(<generated>) ~[spring-cloud-gcp-autoconfigure-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration$$EnhancerBySpringCGLIB$$375b81d4.googleCredentials(<generated>) ~[spring-cloud-gcp-autoconfigure-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
我试图在调试模式下评估排泄,一切正常。为此,我在 Intellij Idea 中评估了这条线。
is = new FileInputStream("C:\\Users\\Admin\\dev\\Projects\\cloud\\credentials-file.json");
在这个方法中
@Override
public InputStream getInputStream() throws IOException {
InputStream is = this.servletContext.getResourceAsStream(this.path);
if (is == null) {
throw new FileNotFoundException("Could not open " + getDescription());
}
return is;
}
解决方案
我将凭据文件移动到文件夹resources/static/my-credentials-file.json
并更改了文件中的文件路径application.properties
(位于src/resources
文件夹中)。
spring.cloud.gcp.credentials.location=file:src/main/resources/static/my-credentials-file.json
spring.cloud.gcp.project-id=my-project-id
解决方案
推荐阅读
- javascript - 删除元素等于数组中元素的对象元素
- nagios - 如何将 json 文件从 nagios 转换为 rrd 以使用 rrdgraph 进行绘图?
- alert - 在异常情况下发出警报
- ubuntu - 单元服务未正确加载:执行格式错误。在 ubuntu18.04 上创建 .net 服务
- javascript - 我的密钥需要采用什么格式才能使用 Node.js Crypto 模块?
- c++ - 找到数字之和等于 N 的最小幸运数字
- php - PHP txt 一行到新数组
- css - 如何阻止元素流出弹性框元素的底部?
- javascript - 如果检查的值在嵌套数组中,如何获取数组的索引?
- javascript - 使用非传统装饰器时如何装饰 JavaScript 类构造函数?