从Spring引导提供静态内容会导致404白标错误页面

时间:2015-12-10 13:00:49

标签: java spring spring-boot

我有一个关于Spring启动的问题。我想提供静态内容,我在配置中创建了ResourceHandler

@Configuration
public class WebApplicationConfig extends WebMvcConfigurerAdapter {

    private static final Logger LOG = LoggerFactory.getLogger(WebApplicationConfig.class);

    @Bean
    public FileSystem fileSystem() {
        FileSystem fileSystem = new LocalFileSystem();
        return fileSystem;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        LOG.info("Serving static content from " + fileSystem().getBaseFolder());
        registry.addResourceHandler("/static/storage/**").addResourceLocations("file:///" + fileSystem().getBaseFolder());
        super.addResourceHandlers(registry);
    }

}

我的应用程序在Docker容器中运行,我安装了主机'volume / myapplication / storage。当我进入我的Docker容器时,我看到该卷存在且反映了fileSystem().getBaseFolder() - 调用。这个目录也是可写的,但它不在我的项目中。

执行LOG语句: "Serving static content from /myapplication/storage",所以我知道这段代码也在执行。

可能相关的更多信息是:

  • 这是一个Linux环境。
  • 该文件夹的写入权限正确。
  • 文件夹存在且可写和可读。
  • 我有几个@RestController的注释。其中的所有方法都以@RequestMapping("/api/")开头,因此没有以/ static开头的映射。
  • 已经从文件前面的///下线了,但没有任何影响。

有谁知道我如何使该文件夹中的文件可读。它总是返回404.文件夹必须在项目之外,因此不建议将其放入/resources/static

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

好的,我犯了初学者的错误。正如@chrylis在评论中提到的那样,我删除了///前面的file:。我做了一些测试,看起来,我在application.propertiesmyapplication.picture.basefolder=/myapplication/storage中输入了以下条目。它错过了一个尾随斜线。所以我将其更新为myapplication.picture.basefolder=/myapplication/storage/,我的问题已修复。