Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 5 - How to provide static resources

I am trying provide static resources in my web application and I tried:

@SuppressWarnings("deprecation")
@Bean
WebMvcConfigurerAdapter configurer(){
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addResourceHandlers (ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**").
                      addResourceLocations("classpath:/static/");
        }
    };
}

BUT WebMvcConfigurerAdapter is deprecated in Spring 5. How can I access the static resources now?

like image 938
John Mendes Avatar asked Oct 04 '17 14:10

John Mendes


1 Answers

Spring 5 - Static Resources

From the documentation:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/resources/**")
                        .addResourceLocations("/public", "classpath:/static/")
                        .setCachePeriod(31556926);
        }

}
like image 50
alfcope Avatar answered Sep 19 '22 06:09

alfcope