SpringBoot集成Swagger无控制器/端点显示问题求助
SpringBoot集成Swagger空白页问题解决方案
核心问题分析
你的代码存在两个关键缺失:
- 未启用Swagger2并配置核心扫描规则
- 缺少Swagger-ui静态资源的映射配置
具体修改步骤
1. 修改主启动类,启用Swagger并配置扫描规则
在StoreApplication类中添加@EnableSwagger2注解,同时定义Docket Bean指定控制器扫描路径:
package com.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.Bean; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication(scanBasePackages = {"com.app.controller"}) @EnableSwagger2 // 启用Swagger2功能 public class StoreApplication extends SpringBootServletInitializer { public static void main(String[] args) { try { SpringApplication.run(StoreApplication.class, args); }catch (Throwable throwable){ System.out.println(throwable.toString()); throwable.printStackTrace(); } } // 配置Swagger核心Bean,指定扫描控制器的包 @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.app.controller")) .build(); } }
2. 修改应用配置类,添加静态资源映射
Swagger-ui的页面资源需要Spring MVC正确映射,在AppConfig中补充addResourceHandlers方法:
package com.app.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class AppConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry){ registry.addViewController("/welcome").setViewName("Welcome"); } // 配置Swagger静态资源访问路径 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
3. 检查依赖配置(关键)
确保项目中已引入Swagger2的正确依赖,以Maven为例,在pom.xml中添加:
<!-- Swagger2 核心依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- Swagger-ui 页面依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
4. 解决SpringBoot版本冲突(若存在)
如果你的SpringBoot版本是2.6及以上,需要在application.properties中添加路径匹配策略配置:
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
备选方案:SpringBoot 3.x 适配
若使用SpringBoot 3.x,由于springfox已停止维护,建议改用springdoc-openapi:
- 替换依赖为:
<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.2.0</version> </dependency>
- 移除
@EnableSwagger2注解,访问地址改为http://localhost:8080/swagger-ui/index.html
验证修改
重启项目后访问http://localhost:8080/swagger-ui.html,即可看到CustomersController中的端点信息。
内容的提问来源于stack exchange,提问作者Alee Raza
相关产品推荐
相关产品推荐

