2

I would like to make Spring Boot's embedded Tomcat to serve static contents from a directory specified by an absolute path /path. I know two methods: via application.properties or programmatically, but they both seem to have side effects. The first method:

spring.resources.static-locations=file:/path

adds /path as requested, but removes (at least) all src/main/resources/resources from any Maven project involved. I could complete the above with

spring.resources.static-locations=file:/path,file:src/main/resources/resources

but it adds only src/main/resources/resources from the main project, not the respective directories having the same relative path in libraries.

Then, there is programmatic approach:

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceHandler("/resources/**")
          .addResourceLocations("/resources/"); 
    }
}

but it uses @EnableWebMvc which is discouraged. Another programmatic approach uses resource handlers but then there is again @EnableWebMvc. These example also contains paths which look relative, like /WEB-INF/classes/content/, but they start with a slash anyway, which poses the question how to add an absolute path.

Is there a method of non-disruptive adding of an absolute directory with static resources to Spring Boot?

scriptfoo
  • 476
  • 5
  • 17

0 Answers0