从文件系统提供静态资源| 春季启动网
问题内容:
我尝试使用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);
}
如何提供相对位置?绝对的道路将使我在构建和部署过程中的生活变得艰难。
问题答案:
file:///
是指向文件系统根目录的绝对URL,因此,这file:///./ext-resources/
意味着Spring Boot ext- resources
在根目录下的目录中查找资源。
更新您的配置以使用诸如file:ext-resources/
URL之类的内容。