spring-mvc - 从文件系统提供静态资源| Spring 启动网

标签 spring-mvc spring-boot

我尝试使用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/

相关文章:

java - Spring MVC 和 Hibernate 集成

java - 一些 Spring 模型属性未显示在我的 JSP 中

java - 在运行时,有没有办法检查哪个 API/ Controller 方法导致 Spring Boot 应用程序中的当前执行点?

javascript - 如何用angular js创建一个线程

spring-boot - 使用gradle将Zip文件上传到nexus

java - org/apache/cxf/jaxb/JAXBToStringStyle 上的 NoClassDefFoundError

android - 将 MultiPart 文件从 Android 上传到服务器(SpringMVC)

java - JPA 调用 MSSQL 存储过程返回重复记录

java - Spring 启动 : Export identifier encountered more than once

spring-boot - Spring Boot使用基本身份验证连接到Elasticsearch