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.

2
  • 1
    Does this answer your question? Spring Boot not serving static content Commented Oct 20, 2021 at 16:25
  • Try to place the logo at src/main/resources/META-INF/resources/ and drop that springboot config Commented Oct 21, 2021 at 7:56

2 Answers 2

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:/

Sign up to request clarification or add additional context in comments.

Comments

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/");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.