【问题标题】:How to change resource type from ServletContextResource to FileSystemResource in Spring如何在 Spring 中将资源类型从 ServletContextResource 更改为 FileSystemResource
【发布时间】:2026-02-20 07:40:01
【问题描述】:

我在 Spring Boot 中打开证书存储时遇到问题。我的应用是一个战争文件,证书存储在外部路径上。

我得到的错误是: nested exception is java.lang.IllegalStateException: Resource 'ServletContext resource [/"file:C:/cert/client-keystore.jks"]' must be on a file system

我将此错误理解为 Spring 试图打开 ServletContextResource,而我猜它应该是 FileSystemResource。如何强制 Spring 重新分类资源?

文件在那里:

从应用程序属性中读取的路径为:

spring.kafka.producer.ssl.key-store-location="file:C:/cert/client-keystore.jks"

文件被读取为: @PropertySource(value = { "file:c:/path/application.properties" })

感谢您的帮助。

巴西,顺丰

【问题讨论】:

  • 您确定您的文件client-keystore.jks 位于您的文件系统中吗?当我读到这个错误时,这就是我所理解的。
  • 从属性值中移除"
  • 它在文件系统中。我认为 Spring 正在寻找类路径中的文件。
  • 如果我删除双引号,我仍然得到: Caused by: java.io.FileNotFoundException: file:C:\cert\client-keystore.jks(文件名、目录名或卷标语法不正确)在 java.io.FileInputStream.open0(Native Method) ~[na:1.8.0_211]
  • 删除双引号使其至少被处理为FileResource。但是无法打开该文件。确保该文件位于该位置,并且该文件可以从您正在运行的进程中读取。

标签: java spring spring-boot spring-kafka


【解决方案1】:

将语法更改为: spring.kafka.producer.ssl.key-store-location=C:/cert/client-keystore.jks

从路径中删除 file:

巴西、旧金山

【讨论】: