SpringBoot添加静态自定义文件夹

时间:2019-04-12 19:50:37

标签: spring-boot configuration static staticresource

我正在使用SpringBoot 2.1.3。我想在静态文件夹中保存一些文件,以方便地通过以下方式访问它:

localhost:8080/[filename]

默认情况下,Spring Boot从类路径中以下位置之一提供静态内容:

1) /static
2) /public
3) /resources
4) /META-INF/resources

如果我在项目类路径中创建公用文件夹,则有效:

enter image description here

并在其中放入file.txt,启动应用程序后,我可以使用上述路径显示文件。

在Google上搜索,我发现了如何以两种方式添加自定义静态文件夹。首先是 application.properties

spring.resources.static-locations=classpath:/documents/

但是,即使我在公用文件夹所在的位置创建了 documents 文件夹,也无法使用以下路径访问该文件:

localhost:8080/[filename]

第二种方法是通过Java @Bean配置:

@Configuration
@EnableWebMvc
public class AppConfiguration implements WebMvcConfigurer {
 @Bean
 WebMvcConfigurer configurer () {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addResourceHandlers (ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/pages/**").
                      addResourceLocations("classpath:/documents/");
        }
    };
 }
}

现在,如果我尝试通过以下方式获取资源:

localhost:8080/pages/[filename]

根本不起作用。

任何提示?

谢谢大家

0 个答案:

没有答案
相关问题