IIWAB springboot全局异常处理设置add-mappings设置false导致swagger也不能访问 - IIWAB

springboot全局异常处理设置add-mappings设置false导致swagger也不能访问

IIWAB 1年前 ⋅ 601 阅读

原因分析

spring.resources.add-mappings=false 为静态资源设置默认处理

spring.mvc.throw-exception-if-no-handler-found=true

这样可以将自定义全局404异常方便Restful使用

但是spring.resources.add-mappings=false会导致swagger也不能访问。

解决方法

在实现WebMvcConfigurer这个接口的类中加入下面的方法实现,指定swagger的静态资源处理

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/", "/static", "/public");

    registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
}

全部评论: 0

    我有话说: