You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot集成Swagger时出现NullPointerException问题求助

问题原因

你碰到的NullPointerException是因为Spring Boot 2.4.17(依赖Spring 5.3.x)和Springfox 2.4.0版本不兼容。Spring 5.3调整了RequestMappingInfo的内部逻辑,部分场景下getPatternsCondition()会返回null,而老版本Springfox(2.4.0)没处理这种null情况,导致排序时触发空指针异常。

解决方案

有两种可行的解决办法,优先推荐第二种:

方案一:升级Springfox到兼容版本

把Springfox依赖升级到2.10.0(这是Springfox 2.x系列的最后稳定版,对Spring 5.3兼容性更好):

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.10.0</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.10.0</version>
</dependency>

升级后重启项目,空指针问题应该能解决。

方案二:替换为Springdoc OpenAPI(更推荐)

Springfox已经停止维护,Springdoc OpenAPI是官方认可的替代方案,完全兼容Spring Boot 2.x及以上版本:

  1. 删除项目里原有的Springfox依赖
  2. 添加Springdoc依赖:
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.13</version> <!-- 适配Spring Boot 2.4.x的稳定版本 -->
</dependency>
  1. 无需额外配置(默认自动生效),访问http://localhost:8080/swagger-ui.html就能查看接口文档。
额外提示

如果用方案一升级后还是有问题,检查项目里有没有自定义的RequestMappingHandlerMapping或者特殊路由配置,确保没覆盖Spring默认逻辑导致PatternsRequestCondition为null。

内容的提问来源于stack exchange,提问作者Naveen Chowdary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 17:02:58