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/
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值