使用 Spring Boot Web 应用程序,我尝试从项目外部的文件系统文件夹中提供静态资源。
文件夹结构如下:-
src
main
java
resources
test
java
resources
pom.xml
ext-resources (I want to keep my static resources here)
test.js
弹簧配置:-
@SpringBootApplication
public class DemoStaticresourceApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(DemoStaticresourceApplication.class, args);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/test/**").addResourceLocations("file:///./ext-resources/")
.setCachePeriod(0);
}
}
在我的浏览器中点击“ http://localhost:9999/test/test.js ”会返回 404。
我应该如何配置 ResourceHandlerRegistry 以提供上述“ext-resources”文件夹中的静态资源?
我应该能够为开发/生产环境打开/关闭缓存。
谢谢
更新 1
提供绝对文件路径有效:-
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/test/**")
.addResourceLocations(
"file:///C:/Sambhav/Installations/workspace/demo-staticresource/ext-resources/")
.setCachePeriod(0);
}
如何提供相对位置?在构建和部署过程中,绝对路径将使我的生活变得艰难。
原文由 Kumar Sambhav 发布,翻译遵循 CC BY-SA 4.0 许可协议
file:///
是指向文件系统根目录的绝对 URL,因此,file:///./ext-resources/
表示 Spring Boot 在名为ext-resources
的目录中寻找资源根。更新您的配置以使用类似
file:ext-resources/
作为 URL。