背景
最近项目从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