从文件系统提供静态资源 |Spring Boot Web [英] Serving Static resources from file system | Spring Boot Web
问题描述
使用 Spring Boot Web 应用程序,我尝试从项目外的文件系统文件夹中提供静态资源.
Using a Spring Boot web application I trying to serve my static resource from a file system folder outside my project.
文件夹结构如下:-
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.
Hitting 'http://localhost:9999/test/test.js' in my browser gives back a 404.
我应该如何配置 ResourceHandlerRegistry 以提供来自上述ext-resources"文件夹的静态资源?
How should I configure ResourceHandlerRegistry to serve static resources from the above mentioned 'ext-resources' folder?
我应该能够为开发/生产环境打开/关闭缓存.
I should be able to switch cache on/off for dev/prod environment.
谢谢
更新 1
给出绝对文件路径有效:-
Giving absolute file path works:-
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/test/**")
.addResourceLocations(
"file:///C:/Sambhav/Installations/workspace/demo-staticresource/ext-resources/")
.setCachePeriod(0);
}
我如何提供相对位置?绝对路径将使我在构建过程中生活艰难部署流程.
How can I provide relative location? Absolute path will make my life tough during build & deploy process.
推荐答案
file:///
是指向文件系统根目录的绝对 URL,因此 file:///./ext-resources/
表示 Spring Boot 正在根目录下一个名为 ext-resources
的目录中寻找资源.
file:///
is an absolute URL pointing to the root of the filesystem and, therefore, file:///./ext-resources/
means that Spring Boot is looking for resources in a directory named ext-resources
in the root.
更新您的配置以使用诸如 file:ext-resources/
之类的内容作为 URL.
Update your configuration to use something like file:ext-resources/
as the URL.
这篇关于从文件系统提供静态资源 |Spring Boot Web的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!