通过继承WebMvcConfigurationSupport配置静态资源映射不生效的问题

原始写法如下

@Configuration
@Slf4j
public class WebConf extends WebMvcConfigurationSupport {

    @Value("${ees.static.path}")
    private String staticPath;
    @Value("${ees.swagger.enable}")
    private Boolean swaggerEnable;

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    	log.info("====================================="+swaggerEnable);
        log.info("====================================="+staticPath);
        if (swaggerEnable == null || swaggerEnable) {
            registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/", "classpath:/META-INF/resources/webjars/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/", "classpath:/META-INF/resources/webjars/");
        }              .addResourceLocations(staticPath);
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

}

配置完之后发现配置不生效日志也不打印~
后续排查发下
WebConf类继承自WebMvcConfigurationSupport,这可能会覆盖Spring Boot的自动配置。尽量避免直接继承WebMvcConfigurationSupport,而是实现WebMvcConfigurer接口,并使用@Configuration注解。
后续修改为如下写法:

@Configuration
@Slf4j
public class WebConf implements WebMvcConfigurer {

    @Value("${ees.static.path}")
    private String staticPath;
    @Value("${ees.swagger.enable}")
    private Boolean swaggerEnable;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("====================================="+swaggerEnable);
        log.info("====================================="+staticPath);
        // swagger访问配置
        if (swaggerEnable == null || swaggerEnable) {
            registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/", "classpath:/META-INF/resources/webjars/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/", "classpath:/META-INF/resources/webjars/");
        }
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

}

修改后正常运行!配置生效!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值