首页 > 解决方案 > 无法在 Spring Boot 应用程序中打开 ServletContext 资源

问题描述

我创建了 Spring Boot 应用程序,它将连接到 Google Cloud pub/sub 并从主题中获取数据。对于身份验证,我已spring.cloud.gcp.credentials.location=C:\\Users\\Admin\\dev\\Projects\\cloud\\credentials-file.jsonapplication.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

标签: spring-bootservletsgoogle-cloud-platform

解决方案


推荐阅读