[英]Spring boot fat jar cannot read file from absolute path defined in application.properties
So i have a Spring boot fat jar that has a config that needs to load a .pem
file from an absolute path defined in application.properties
file.所以我有一个 Spring boot fat jar,它有一个配置,需要从
application.properties
文件中定义的绝对路径加载.pem
文件。
I defined the path like this:我定义了这样的路径:
security.jwt.public-key=/opt/samara/staging.pem
The usage is like this:用法是这样的:
@Value("\${security.jwt.public-key}")
lateinit var publicKey: Resource
Here is the directory structure:这是目录结构:
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
But when i tried to run it like this:但是当我尝试像这样运行它时:
root@samara:/opt/samara# java -jar app.jar
it failed with this:它失败了:
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]
Actually, i copied those files from another machine and it runs perfectly in that machine but i don't know why it failed in the new machine.实际上,我从另一台机器复制了这些文件,它在该机器上运行良好,但我不知道为什么它在新机器上失败。 Thanks.
谢谢。
After reading this: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#resources阅读本文后: https : //docs.spring.io/spring/docs/current/spring-framework-reference/core.html#resources
I changed the path definition to:我将路径定义更改为:
security.jwt.public-key=file:/opt/samara/staging.pem
And it worked!它奏效了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.