SpringBoot 从自定义路径提供静态图像

SpringBoot serving static images from custom Path

像现在一样,我一直通过 application.properties 文件中的静态路径提供我的图像:

spring.resources.staticlocations=file:/Applications/MAMP/htdocs/reportMaker/template
spring.mvc.static-path-pattern=/resources/**

然后通过

http://localhost:8080/resources/logo.png

我可以到达徽标。

现在我的目标是使用从我的数据库中获取的文件夹路径进行切换。

我试过这种方法:

@EnableWebMvc
@Configuration
public class StaticResourceConfiguration implements WebMvcConfigurer {

@Autowired
ConfigurationRepository confRepo;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    myConfiguration conf = confRepo.findByConfKey("downloadPath");
    String path =  conf.getConfValue();

    if(path !=null) {
        registry.addResourceHandler("/resources/**").addResourceLocations(path);
    }
}

但是我无法像以前一样访问徽标。

路径变量为/Applications/MAMP/htdocs/reportMaker/template

The path variable is /Applications/MAMP/htdocs/reportMaker/template.

根据此文档 https://www.baeldung.com/spring-mvc-static-resources 路径应以 file:/

为前缀

我已通过删除:@EnableWebMvc 并在我的路径末尾添加 / 来解决

@Configuration
public class StaticResourceConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("file:/Applications/MAMP/htdocs/reportMaker/template/");
    }
}