【总结】springboot 2.7.x集成springdoc openapi 实现api文档快速生成

在将Springboot从2.3.x升级到2.7.12后,遇到Swagger无法使用的问题,报NullPointerException。尝试多种配置修复未果。根据GitHub上的issue,了解到springfox可能不再维护,建议使用springdoc。按照springdoc的文档,只需添加pom依赖和简单配置,即可顺利集成并解决Swagger问题。需要注意的是,要正确设置server.servlet.context-path来避免baseurl的错误提示。

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

背景

最近项目从springboot 2.3.x 升到了2.7.12,发现swagger 不能用了,一直报下面的错:

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)
	……
	at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:164)
	at cn.xxxx.yyy.qiming.app.biz.AppMain.main(AppMain.java:82)
Caused by: java.lang.NullPointerException: null
	at springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper.getPatterns(WebMvcPatternsRequestConditionWrapper.java:56)
	at springfox.documentation.RequestHandler.sortedPaths(RequestHandler.java:113)

试过了网上说的,添加spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER ,以及各种配置config都没有解决。

在排查的过程中,找到github上的一个issues,https://github.com/springfox/springfox/issues/3462 这篇文章里说:springfox 已死,让我们转用springdoc。于是胡开始研究springdoc,发现springdoc 集成起来非常简洁好用。

springdoc 文档:https://springdoc.org/#Introduction
快速开始:https://springdoc.org/#getting-started

快速开始

真的很简单,只需两步即可。

添加pom依赖

	    <dependency>
			<groupId>org.springdoc</groupId>
			<artifactId>springdoc-openapi-ui</artifactId>
			<version>1.7.0</version>
		</dependency>

添加配置项

server.servlet.context-path=/api

访问

访问地址:http://localhost:8080/api/swagger-ui/index.html

报错信息

问题1:Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:

添加该配置即可解决:server.servlet.context-path=/api
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长乐smile

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值