解决Spring Boot 2.6+ 与Springfox3.0.0 不兼容

启动SpringBoot报,报错信息:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" because the return value of "springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper._jr$ig$condition(Object)" is null
2022-08-25 17:44:39.844  WARN 16760 --- [           main] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [oracle.jdbc.driver.BlockSource.ThreadedCachingBlockSource.BlockReleaser] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.base@17.0.4.1/jdk.internal.misc.Unsafe.park(Native Method)
java.base@17.0.4.1/java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:252)
java.base@17.0.4.1/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:1672)
app//oracle.jdbc.internal.Monitor$WaitableMonitor.monitorWait(Monitor.java:258)
app//oracle.jdbc.internal.Monitor$WaitableMonitor.monitorWait(Monitor.java:240)
app//oracle.jdbc.driver.BlockSource$ThreadedCachingBlockSource$BlockReleaser.run(BlockSource.java:345)
核查

项目使用了Springboot2.7、knife4j3(springfox3.0.0)、Redission27版,经过苦苦排查,是由于Springboot 2.6+ 与springfox3.0.0 不兼容导致,最终还是经过大佬找资料+指导,添加以下配置解决:

1.SpringBoot配置文件添加配置:
spring:
  mvc:
    pathmatch:
      # https://doc.xiaominfo.com/docs/quick-start
      matching-strategy: ant_path_matcher
2.添加配置启动代码,可以在swagge或者knife4j配置类加上:

添加1配置后,knife4j能起来了,但是用了Redission27一样启动报**Failed to start bean ‘documentationPluginsBootstrapper’**异常,需要继续添加配置处理

@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(
        WebEndpointsSupplier webEndpointsSupplier,
        ServletEndpointsSupplier servletEndpointsSupplier,
        ControllerEndpointsSupplier controllerEndpointsSupplier,
        EndpointMediaTypes endpointMediaTypes,
        CorsEndpointProperties corsProperties,
        WebEndpointProperties webEndpointProperties,
        Environment environment) {
    List<ExposableEndpoint<?>> allEndpoints = new ArrayList<ExposableEndpoint<?>>();
    Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
    allEndpoints.addAll(webEndpoints);
    allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
    allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
    String basePath = webEndpointProperties.getBasePath();
    EndpointMapping endpointMapping = new EndpointMapping(basePath);
    boolean shouldRegisterLinksMapping =
            this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
    return new WebMvcEndpointHandlerMapping(
            endpointMapping,
            webEndpoints,
            endpointMediaTypes,
            corsProperties.toCorsConfiguration(),
            new EndpointLinksResolver(allEndpoints, basePath),
            shouldRegisterLinksMapping,
            null);
}

private boolean shouldRegisterLinksMapping(
        WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
    return webEndpointProperties.getDiscovery().isEnabled()
            && (StringUtils.hasText(basePath)
            || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}

吐槽springfox3的同时,完美启动项目。

### Spring Boot Springfox 的版本兼容性对照表 在开发基于 Spring Boot 的项目时,确保 Springfox 版本 Spring Boot 版本兼容是至关重要的。以下是一个详细的 Spring Boot Springfox 版本兼容性对照表[^1]: | Spring Boot 版本 | Springfox 版本 | 备注 | |------------------|-------------------------|----------------------------------------------------------------------| | 2.7.x | 3.0.0 | 推荐使用 Springfox 3.x 系列以获得最佳支持和功能[^2] | | 2.6.x | 3.0.0 | Springfox 3.x 支持 OpenAPI 3.0 规范[^2] | | 2.5.x | 3.0.0 | 确保正确配置 `springfox-boot-starter` 依赖 | | 2.4.x | 3.0.0 | 避免使用 Springfox 2.x,因为其不支持 Spring Boot 2.4+ | | 2.3.x | 3.0.0 | 如果遇到问题,可以尝试降级到 Springfox 2.9.2[^3] | | 2.2.x | 2.9.2 | Springfox 2.x 系列支持 Spring Boot 2.2.x 及更早版本[^3] | | 2.1.x | 2.9.2 | 确保正确配置 `Docket` Bean 和扫描路径[^3] | | 2.0.x | 2.8.0 | 使用 Springfox 2.8.0 或更低版本以避免兼容性问题 | | 1.5.x | 2.7.0 | 对于 Spring Boot 1.5.x,推荐使用 Springfox 2.7.0 或更低版本 | #### 示例 Maven 配置 以下是针对不同版本的 Spring BootSpringfox 的 Maven 配置示例: 对于 Spring Boot 2.7.x: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 对于 Spring Boot 2.3.x: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> ``` #### 注意事项 - 在使用 Springfox 3.x 时,建议使用 `springfox-boot-starter` 而不是单独引入 `springfox-swagger2` 和 `springfox-swagger-ui`[^2]。 - 如果项目中存在其他第三方库(如 Jackson、Hibernate Validator 等),需要确保它们的版本也 Spring BootSpringfox 兼容[^3]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值