我尝试使用Spring Boot Web应用程序从项目外部的文件系统文件夹中提供静态资源。
文件夹结构如下:-
src
main
java
resources
test
java
resources
pom.xml
ext-resources (I want to keep my static resources here)
test.js
Spring 配置:-
@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。
关于spring-mvc - 从文件系统提供静态资源| Spring 启动网,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28556300/