Spring Boot Fat Jar无法从application.properties中定义的绝对路径读取文件

时间:2019-11-27 17:03:48

标签: java spring spring-boot

因此,我有一个Spring Boot胖子,其配置需要从.pem文件中定义的绝对路径加载application.properties文件。

我这样定义路径:

security.jwt.public-key=/opt/samara/staging.pem

用法如下:

    @Value("\${security.jwt.public-key}")
    lateinit var publicKey: Resource

这是目录结构:

root@samara:/opt/samara# ls -lsh
-rwxr-xr-x 1 root root  44M Nov 27 07:50 app.jar
-rwxr-xr-x 1 root root 2.0K Nov 27 07:53 staging.pem
-rwxr-xr-x 1 root root 1.2K Nov 27 08:29 application.properties

但是当我试图像这样运行它时:

root@samara:/opt/samara# java -jar app.jar

此操作失败:

Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/opt/samara/staging.pem]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:158) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]

实际上,我从另一台计算机上复制了这些文件,并且该文件在该计算机上运行完美,但是我不知道为什么它在新计算机上失败。谢谢。

1 个答案:

答案 0 :(得分:0)

阅读以下内容: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#resources

我将路径定义更改为:

security.jwt.public-key=file:/opt/samara/staging.pem

成功了!