spring boot + swagger报错记录

本文讲述了Spring Boot 2.4项目中由于版本不兼容导致与Swagger 2.2.2集成失败的问题,通过将Swagger版本更新到2.9.2解决了启动报错,关键在于版本对应和配置调整。

springboot 整合 swagger2 报错,项目不能启动


APPLICATION FAILED TO START


Description:

Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration required a single bean, but 15 were found:
- modelBuilderPluginRegistry: defined in null
- modelPropertyBuilderPluginRegistry: defined in null
- typeNameProviderPluginRegistry: defined in null
- documentationPluginRegistry: defined in null
- apiListingBuilderPluginRegistry: defined in null
- operationBuilderPluginRegistry: defined in null
- parameterBuilderPluginRegistry: defined in null
- expandedParameterBuilderPluginRegistry: defined in null
- resourceGroupingStrategyRegistry: defined in null
- operationModelsProviderPluginRegistry: defined in null
- defaultsProviderPluginRegistry: defined in null
- pathDecoratorRegistry: defined in null
- relProviderPluginRegistry: defined by method ‘relProviderPluginRegistry’ in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]
- linkDiscovererRegistry: defined in null
- entityLinksPluginRegistry: defined by method ‘entityLinksPluginRegistry’ in class path resource [org/springframework/hateoas/config/WebMvcEntityLinksConfiguration.class]

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

Process finished with exit code 0

原因:

spring boot与swagger的版本冲突了,这里我springboot项目的版本高了,swagger版本低了,不兼容。我的springboot版本为2.4,swagger版本为2.2.2,项目启动就会报如下错误:(这个问题的解决办法就是直接将pom文件中的swagger版本换成2.9.2就可以了)

### 解决 Spring Boot 项目中引入 Swagger 报错问题 在 Spring Boot 2.6.x 版本中,由于框架内部机制的变化以及依赖冲突等问题,在引入 `springfox-boot-starter` (即 Swagger 的实现) 后可能会遇到多种报错情况。以下是针对常见报错的解决方案。 #### 常见报错及其原因分析 1. **Failed to start bean ‘documentationPluginsBootstrapper’** 这种错误通常是因为 Spring Boot 2.6.x 默认禁用了 Bean 的循环引用检测功能[^1]。此外,Spring MVC 请求路径匹配器也发生了变化,从 `AntPathMatcher` 转变为 `PathPatternParser`,这可能导致某些旧版 Swagger 配置不兼容[^2]。 2. **NullPointerException 或其他运行时异常** 如果未正确配置 Swagger 或者存在版本不一致的情况,则可能引发 NullPointerException 等运行时异常[^3]。 --- #### 解决方案 ##### 方法一:调整 Maven 依赖并升级版本 确保使用的 `springfox-boot-starter` 是最新稳定版本,并与当前项目的 Spring Boot 版本保持兼容性。例如: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 如果仍然存在问题,可以尝试切换到更高版本的 `springfox-boot-starter`,或者考虑使用 OpenAPI 工具替代 Swagger。 ##### 方法二:启用循环引用支持 对于因默认禁用循环引用而导致的问题,可以通过设置属性重新允许循环引用。修改 `application.properties` 文件如下: ```properties spring.main.allow-circular-references=true ``` 此操作可有效解决部分由循环引用引起的启动失败问题。 ##### 方法三:更改 Path Pattern Parser 行为 为了适配新的路径解析逻辑,可以在应用初始化阶段强制恢复至旧版行为 (`AntPathMatcher`)。通过自定义配置类完成这一目标: ```java import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void configurePathMatch(PathMatchConfigurer configurer) { configurer.setUseRegisteredSuffixPatternMatch(true); configurer.useSuffixPatternMatch(false); // 关闭后缀模式匹配 configurer.setPathMatcher(new AntPathMatcher()); // 使用传统方式 } } ``` 上述代码片段将路径匹配器重设为传统的 `AntPathMatcher` 实现。 ##### 方法四:排查 NPE 及其他潜在问题 当发生 NullPointer Exception 时,需重点检查以下几个方面: - 是否遗漏了必要的注解(如 `@EnableSwagger2` 或其等价形式); - API 文档扫描包范围是否覆盖实际接口所在的模块; - Docket 配置是否存在语法错误或参数缺失。 典型配置示例: ```java import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SwaggerConfiguration { @Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiDetails()) .select() .build(); } private ApiInfo apiDetails() { return new ApiInfoBuilder().title("My Application API").description("Documentation for My App") .version("1.0").build(); } } ``` 以上代码展示了如何创建一个基本的 Docket 对象实例。 --- ### 总结 综合来看,Spring Boot 2.6.x 中集成 Swagger 所产生的各类问题主要源于新特性带来的向后兼容性挑战。通过对依赖管理、核心配置文件调整以及必要扩展手段的应用,能够显著降低此类风险的发生概率。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值