Spring Boot集成Swagger-ui遇No mapping for GET /swagger-ui.html报错求助
解决Spring Boot + Springfox Swagger 3.0.0 访问Swagger UI 404问题
核心问题分析
你遇到的404错误主要来自两个点:@EnableWebMvc禁用了Spring Boot默认的静态资源自动配置,以及Swagger 3.0.0的UI访问路径已发生变更。
解决方案
移除启动类上的
@EnableWebMvc注解
Spring Boot中添加@EnableWebMvc会完全接管MVC配置,关闭默认的静态资源映射规则,而Swagger UI的静态资源依赖这些默认配置才能被正常访问。移除该注解后,Spring Boot会自动处理静态资源的映射,Swagger UI的资源就能正常加载。使用正确的Swagger UI访问路径
Springfox Swagger 3.0.0不再使用/swagger-ui.html作为入口,正确的访问路径是:http://localhost:8080/swagger-ui/(注意末尾必须带斜杠)- 或者
http://localhost:8080/swagger-ui/index.html
修正Swagger配置类的注解
Swagger 3.0.0对应OpenAPI 3规范,配置类需要使用@EnableOpenApi注解替代旧版本的@EnableSwagger2,示例配置如下:@Configuration @EnableOpenApi public class CoreConfiguration { @Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .select() // 指定扫描的控制器包路径 .apis(RequestHandlerSelectors.basePackage("com.yourpackage.controller")) .paths(PathSelectors.any()) .build(); } }优化依赖引入方式
建议使用springfox-boot-starter替代单独引入springfox-swagger2和springfox-swagger-ui,starter会自动处理依赖关系和基础配置,避免版本冲突:<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
内容的提问来源于stack exchange,提问作者Mateusz Kuncewicz
相关产品推荐
相关产品推荐

