总结错误 No mapping for GET /swagger-ui.html

在SpringBoot项目引入Swagger2后,在浏览器地址里输入地址:http://ip:port/swagger-ui.html 报错:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Dec 06 15:48:30 CST 2021

There was an unexpected error (type=Not Found, status=404).

No message available

查看后台控制台,发现这样的错误:

o.s.web.servlet.PageNotFound             : No mapping for GET /swagger-ui.html

经过分析发现由于项目中有配置注解类(@Configuration)继承了WebMvcConfigurationSupport,导致默认的Swagger静态资源被覆盖,而缺失了配置。

可在该继承配置类中,显式添加如下swagger静态资源:

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

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

即可解决。

其他,不单单swagger,同样的问题,如果因为继承WebMvcConfigurationSupport导致其他默认配置失效。比如,Controller方法上自定义注解失效,则需要在该继承配置类中重写:

    @Override
    protected void addArgumentResolvers(List argumentResolvers) {
        argumentResolvers.add(...bean...);
    }

你可能感兴趣的:(Spring,spring,boot,swagger2)