Spring Boot 2.x以后static下面的静态资源被拦截

今天创建一个新的Spring Boot项目,没注意到spring boot的版本,发现静态资源无法访问。百度一下发现好像是Spring Boot 2.0版本以后static目录不能直接访问。
接下来直接上解决方法。

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 静态资源映射
 */
@Component
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }
}
posted @ 2019-03-12 15:33  Duo17  阅读(783)  评论(0编辑  收藏  举报