1.使用的版本
Swagger版本:2.9.2
Spring Boot版本:2.6.15
2.问题
(1)控制台报错
o.s.web.servlet.PageNotFound - No mapping for GET /swagger-ui.html
WARN o.s.web.servlet.PageNotFound - No mapping for GET /swagger-ui.html
(2)页面报错
This application has no explicit mapping for /error, so you are seeing this as a fallback.

3.问题原因
实现了WebMvcConfigurer接口,重写了addResourceHandlers方法
package com.sdses.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyWebMVCConfig implements WebMvcConfigurer {
@Value("${digital.file.resource-path}")
private String resourcePath;
@Value("${digital.file.base-file-path}")
private String locationPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//匹配到resourceHandler,将URL映射至本地文件夹
registry.addResourceHandler(resourcePath).addResourceLocations("file:///" + locationPath);
}
}
4.解决
经过分析发现由于项目中有配置类(@Configuration)继承了WebMvcConfigurer类 ,重写了addResourceHandlers()方法,导致默认的Swagger静态资源被覆盖,而缺失了配置。需要重新指定静态资源。
可在该继承配置类中,显式添加如下swagger静态资源:
配置Swagger UI和WebJars资源的访问路径
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//匹配到resourceHandler,将URL映射至本地文件夹
registry.addResourceHandler(resourcePath).addResourceLocations("file:///" + locationPath);
// 配置Swagger UI和WebJars资源的访问路径,使得这些静态资源可以通过特定的URL路径在Web应用中被访问
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/");
}
}