1

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.

Pickeroll
  • 908
  • 2
  • 10
  • 25
  • 1
    Does this answer your question? [Spring Boot not serving static content](https://stackoverflow.com/questions/24661289/spring-boot-not-serving-static-content) – Abhijit Sarkar Oct 20 '21 at 16:25
  • Try to place the logo at src/main/resources/META-INF/resources/ and drop that springboot config – Grim Oct 21 '21 at 07:56

2 Answers2

1

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

According to this documentation https://www.baeldung.com/spring-mvc-static-resources the path should be prefixed by file:/

Genu
  • 827
  • 6
  • 12
0

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/");
    }
}
Pickeroll
  • 908
  • 2
  • 10
  • 25