Spring Boot 3中用OpenApi无法打开swagger-ui(白标错误页)求助
Spring Boot中访问Swagger UI出现Whitelabel Error Page(404)的解决方法
针对访问http://localhost:8080/swagger-ui.html出现404错误的问题,结合你的配置信息,给出以下排查和解决步骤:
1. 修正日志配置错误
你的application.properties中配置了logging.level.io.springfox=DEBUG,但springdoc-openapi与springfox是完全独立的Swagger实现库,该配置无法输出springdoc的调试信息。请修改为:
logging.level.org.springdoc=DEBUG
修改后重启项目,可查看springdoc相关初始化日志,确认是否存在加载异常。
2. 检查版本兼容性
springdoc-openapi-ui:1.6.4仅兼容Spring Boot 2.6.x ~ 2.7.x版本:
- 若使用Spring Boot 3.x及以上,需升级springdoc到v2.x版本(依赖调整为
org.springdoc:springdoc-openapi-starter-webmvc-ui) - 若Spring Boot版本低于2.6.x,需降级springdoc到对应兼容版本
3. 调整Swagger UI访问路径
尝试移除application.properties中的springdoc.swagger-ui.path=/swagger-ui.html配置,使用springdoc默认访问路径:
http://localhost:8080/swagger-ui/index.html- 或
http://localhost:8080/swagger-ui/(注意末尾斜杠)
若需自定义路径,确保配置与访问路径一致,例如:
springdoc.swagger-ui.path=/my-swagger.html
此时访问路径应为http://localhost:8080/my-swagger.html
4. 排查静态资源拦截问题
若项目存在自定义WebMvcConfigurer配置,可能拦截了Swagger UI的静态资源,需添加放行规则:
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // 放行swagger-ui静态资源 registry.addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springdoc-openapi-ui/"); // 放行openapi描述文件 registry.addResourceHandler("/v3/api-docs/**") .addResourceLocations("classpath:/META-INF/resources/"); } }
5. 清理依赖并重新构建
依赖包下载不完整可能导致静态资源缺失,执行Maven命令清理并重新构建:
mvn clean install
原始配置信息
pom.xml依赖
<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.6.4</version> </dependency>
OpenApi配置类
@Configuration public class OpenApiConfig { @Bean public OpenAPI customOpenAPI(@Value("${application-description}") String description, @Value("${application-version}") String version) { return new OpenAPI() .info(new Info().title("API") .version(version) .description(description) .license(new License().name("API Licence"))); } }
application.properties
springdoc.swagger-ui.path=/swagger-ui.html application-description=API Description application-version=1.0 logging.level.org.springframework.web=DEBUG logging.level.io.springfox=DEBUG
报错日志
2023-02-09T08:36:16.593+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : GET "/swagger-ui.html", parameters={} 2023-02-09T08:36:16.594+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]] 2023-02-09T08:36:16.596+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found 2023-02-09T08:36:16.596+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND 2023-02-09T08:36:16.597+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={} 2023-02-09T08:36:16.597+03:00 DEBUG 20184 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse) 2023-02-09T08:36:16.599+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8] 2023-02-09T08:36:16.599+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
内容的提问来源于stack exchange,提问作者Sercan Noyan Germiyanoğlu
相关产品推荐
相关产品推荐

