Spring 5 - 如何提供静态资源

Joh*_*des 7 java resources spring spring-mvc spring-webflux

我正在尝试在我的Web应用程序中提供静态资源,我试过:

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

但是在Spring 5中不推荐使用WebMvcConfigurerAdapter.如何立即访问静态资源?

alf*_*ope 10

Spring 5 - 静态资源

从文档:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

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

}
Run Code Online (Sandbox Code Playgroud)