天天看点

springboot2.0以上实现WebMvcConfigurer导致spring.resources.static-locations设置的首页404的问题

//    @Override
    //    public   void addViewControllers(ViewControllerRegistry registry) {
    //        //addViewControllers 会先于addResourceHandlers调用,并执行WelcomePageHandlerMapping读取springboot默认配置
    //        //或者spring.resources.static-locations中的静态路径查找index.html设置为首页,所以必须在addResourceHandlers中设置的
    //        //静态首页目录不会生效,必须在spring.resources.static-locations配置首页所在目录。
    //        // 例如:spring.resources.static-locations=classpath:/dist/(dist目录下有index.html)
    //       //ps:addResourceHandlers会覆盖spring.resources.static-locations的配置导致spring.resources.static-locations内容丢失
    //    }

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //addResourceHandler是指你想在url请求的路径
    //addResourceLocations是图片存放的真实路径
    String path = "file:" + MatUtil.getFilePath(this.getClass()) + "/static/";//这里实现访问与jar同级目录的静态文件
    registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/META-INF/resources/")
            .addResourceLocations("classpath:/resources/")
            .addResourceLocations("classpath:/static/")
            .addResourceLocations("classpath:/public/")
            .addResourceLocations("classpath:/dist/")
            .addResourceLocations(path);
}
           

properties或者yml配置:

##spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/dist/**

上面的其他路径都可以省去,index.html所在的dist目录不能省略

spring.resources.static-locations=classpath:/dist/

springboot2.0以上实现WebMvcConfigurer导致spring.resources.static-locations设置的首页404的问题