Spring boot静态文件加载404

产生原因:1.5 版本前 不拦截静态资源,2.x+版本 拦截静态资源

解决办法:


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class StaticSourceConfig implements WebMvcConfigurer {
    
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/"};


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        try {
            if (!registry.hasMappingForPattern("/webjars/**")) {
                registry.addResourceHandler("/webjars/**").addResourceLocations(
                        "classpath:/META-INF/resources/webjars/");
            }
            if (!registry.hasMappingForPattern("/**")) {
                registry.addResourceHandler("/**").addResourceLocations(
                        CLASSPATH_RESOURCE_LOCATIONS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

列举:
在这里插入图片描述
在这里插入图片描述
加载成功。。。
在这里插入图片描述

发布了22 篇原创文章 · 获赞 9 · 访问量 7657

猜你喜欢

转载自blog.csdn.net/qq_35719898/article/details/103832205