Spring Boot2.x教程:(四)Spring Boot2.6及之后版本整合Knife4j的问题

大家好,我是欧阳方超,可以扫描下方二维码关注我的公众号“欧阳方超”,后续内容将在公众号首发。
在这里插入图片描述

1、概述

今天在2.7.13版本的Spring Boot中使用Knife4j时出现了问题。下面是我引入的依赖:

<dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>3.0.0</version>
            </dependency>
            <!-- knife4j -->
            <dependency>
                <groupId>com.github.xiaoymin</groupId>
                <artifactId>knife4j-spring-boot-starter</artifactId>
                <version>3.0.3</version>
            </dependency>

启动时可能会有报错:

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.23.jar:5.3.23]
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.23.jar:5.3.23]
	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.23.jar:5.3.23]
	at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_201]
	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.23.jar:5.3.23]
	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.23.jar:5.3.23]
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.23.jar:5.3.23]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.23.jar:5.3.23]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.13.jar:2.6.13]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:745) [spring-boot-2.6.13.jar:2.6.13]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:420) [spring-boot-2.6.13.jar:2.6.13]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.6.13.jar:2.6.13]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317) [spring-boot-2.6.13.jar:2.6.13]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) [spring-boot-2.6.13.jar:2.6.13]
	at com.cjbdi.service.ServiceApplication.main(ServiceApplication.java:10) [classes/:na]
Caused by: java.lang.NullPointerException: null
	at springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper.getPatterns(WebMvcPatternsRequestConditionWrapper.java:56) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0]
	at springfox.documentation.RequestHandler.sortedPaths(RequestHandler.java:113) ~[springfox-core-3.0.0.jar:3.0.0]
	at springfox.documentation.spi.service.contexts.Orderings.lambda$byPatternsCondition$3(Orderings.java:89) ~[springfox-spi-3.0.0.jar:3.0.0]

2、问题出现原因及解决办法

新版本 Spring Boot 默认使用 PathPatternParser 进行 URL 路径匹配,而之前的版本使用 AntPathMatcher。想让 Spring Boot 继续使用 AntPathMatcher 进行 URL 路径匹配。要实现这一点,需要在配置文件中修改配置,使其使用 AntPathMatcher 策略。
换言之,为了保持旧版本的路径匹配行为,您通过调整配置将 Spring Boot 的默认路径匹配策略从 PathPatternParser 改回 AntPathMatcher。以下是具体的配置:

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

注意在启动类上加@EnableWebMvc注解的方式只能保证程序可以启动,但是解决不了swagger无法访问的问题,如果从这个角度修改的话需要进一步调整。

3、拓展

Spring Boot 路径匹配策略:AntPathMatcher vs PathPatternParser
在 Spring Boot 应用中,URL 路径匹配是至关重要的一个环节,它决定了请求的路由和处理方式。Spring Boot 提供了两种路径匹配策略:AntPathMatcher 和 PathPatternParser。
AntPathMatcher 是 Spring 框架中传统的路径匹配器,它使用 Ant 风格的通配符来匹配路径,比如 * 代表任意字符,** 代表任意层级的目录。AntPathMatcher 在 Spring Boot 的早期版本中是默认的路径匹配策略。
PathPatternParser 是 Spring 5.0 版本中引入的一种新的路径匹配器,它支持更强大的匹配模式,包括变量提取、命名空间匹配等功能,可以更加灵活地处理复杂路径匹配场景。Spring Boot 2.6 版本开始,PathPatternParser 成为默认的路径匹配策略。

3.1、为什么发生这种变化

为什么要从 AntPathMatcher 切换到 PathPatternParser?
更强大的功能: PathPatternParser 支持更多更灵活的匹配模式,例如变量提取、命名空间匹配等等,可以处理更复杂的路径匹配场景。
更清晰的语法: PathPatternParser 的语法更加简洁直观,更容易理解和维护。
更好的性能: 在一些场景下,PathPatternParser 的性能表现优于 AntPathMatcher。
如何使用 AntPathMatcher?

如果您需要使用 AntPathMatcher 作为您的 Spring Boot 应用的路径匹配策略,您可以在 application.properties 或 application.yml 文件中添加如下配置:

spring.mvc.pathmatch.matching-strategy: ant_path_matcher

4、总结

PathPatternParser 是 Spring Boot 推荐使用的路径匹配策略,它提供了更多功能和更高的灵活度。但是,如果您需要使用 AntPathMatcher,可以通过配置来实现。选择哪种匹配策略取决于您的具体需求和代码的兼容性。
我是欧阳方超,把事情做好了自然就有兴趣了,如果你喜欢我的文章,欢迎点赞、转发、评论加关注。我们下次见。
在这里插入图片描述

  • 15
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值