javaspring-bootstaticresource

SpringBoot serving static images from custom Path


As now I've always served my images through a static path in the application.properties file:

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

Then by doing

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

I'm able to reach the logo.

Now my aim is to switch with a folder path taken from my DB.

I've tried this approach:

@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);
    }
}

But I can't reach the logo in same way as before.

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


Solution

  • I've resolved by removing: @EnableWebMvc and adding a / at the end of my path

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