Add external resources folder to Spring Boot

前端 未结 2 1211
时光说笑
时光说笑 2021-01-04 12:16

I\'d like to add a resource folder relative to the location of the jar (in addition to packaged resources within my jar), for example:

/Directory
    Applica         


        
2条回答
  •  自闭症患者
    2021-01-04 12:31

    Your second approach would work:

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/", "file:resources/");
    }
    

    but only if you launched Spring Boot from /Directory, because file:resources/ is a relative path.

    cd Directory
    java -jar Application.jar
    

    It's nice if you can pack everything into the jar, but if you have to reference external resources, you should use absolute paths to avoid problems like this.

提交回复
热议问题