Spring boot fat jar无法从application.properties中定义的绝对路径读取文件

问题描述 投票:0回答:1

所以我有一个Spring boot fat jar,其配置需要从.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]

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

java spring spring-boot
1个回答
0
投票

阅读完此:https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/resources.html#resources-resource-strings

我试图将路径定义更改为:

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

它成功了!

© www.soinside.com 2019 - 2024. All rights reserved.