Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointer

在SpringBoot2.4.x及以上版本中,由于默认的路径匹配策略PathPatternParser与Springfox不兼容,导致集成Swagger时出现错误。解决方案包括在application.properties中设置`spring.mvc.pathmatch.matching-strategy=ant-path-matcher`来恢复AntPathMatcher策略,或者通过排除SpringBoot的Tomcat依赖,引入Jetty来解决这个问题。但后者可能影响SpringActuator的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个错误是在集成Swagger时出现的。Spring Boot 2.4.x以上版本默认的路径匹配策略是PathPatternParser,而Springfox假设Spring MVC的路径匹配策略是AntPathMatcher,这就导致了错误。下面是解决此问题的方法:

  • 在application.properties文件中添加以下配置,将路径匹配策略更改为AntPathMatcher:
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
  • 在pom.xml中排除spring-boot-starter-web依赖并添加以下依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

这会将Spring Boot的默认嵌入式Tomcat容器替换为Jetty容器,因为Jetty使用AntPathMatcher作为默认的路径匹配策略。

不过使用第一种方法可能会导致无法使用Spring Actuator,因为它使用PathPattern作为URL匹配策略。在这种情况下,可以尝试使用第二种方法,即将Tomcat替换为Jetty容器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值