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/");
    }
}
原文地址:https://www.cnblogs.com/helloDuo/p/10517058.html