最近把项目升级了springboot2.6.1版本,发现启动报错,
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.6.jar:5.3.6]
at org.springframework.context.support.DefaultLifecycleProcessor.access 200 ( D e f a u l t L i f e c y c l e P r o c e s s o r . j a v a : 54 ) [ s p r i n g − c o n t e x t − 5.3.6. j a r : 5.3.6 ] a t o r g . s p r i n g f r a m e w o r k . c o n t e x t . s u p p o r t . D e f a u l t L i f e c y c l e P r o c e s s o r 200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.6.jar:5.3.6] at org.springframework.context.support.DefaultLifecycleProcessor 200(DefaultLifecycleProcessor.java:54) [spring−context−5.3.6.jar:5.3.6]atorg.springframework.context.support.DefaultLifecycleProcessorLifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.6.jar:5.3.6]
at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_77]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.0-20210413.204210-414.jar:2.5.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:772) [spring-boot-2.5.0-20210413.204210-414.jar:2.5.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:441) [spring-boot-2.5.0-20210413.204210-414.jar:2.5.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:339) [spring-boot-2.5.0-20210413.204210-414.jar:2.5.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1350) [spring-boot-2.5.0-20210413.204210-414.jar:2.5.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1339) [spring-boot-2.5.0-20210413.204210-414.jar:2.5.0-SNAPSHOT]
at com.example.demo.DemoApplication.main(DemoApplication.java:12) [classes/:na]
Caused by: java.lang.NullPointerException: null
经过各种尝试发现是springfox还没有适配最新版,解决办法要么降低spring版本,要么根据官方迁移文档使用最新的springdoc。我才用了后者。使用方法很简单删除 springfox 和 swagger 2 依赖项。添加springdoc-openapi-ui依赖项。
implementation(“org.springdoc:springdoc-openapi-ui:1.5.12”)
application.properties添加扫描
springdoc.packagesToScan=package1, package2
注册Bean
@Configuration
class SwaggerConfiguration {
@Bean
fun springShopOpenAPI(): OpenAPI? {
return OpenAPI()
.info(
Info().title("Spring API")
.description("Spring shop sample application")
.version("v1.0")
.license(License().name("Apache 2.0").url("https://springdoc.org"))
)
.externalDocs(
ExternalDocumentation()
.description("SpringShop Wiki Documentation")
.url("https://springshop.wiki.github.org/docs")
)
}
}
更多使用方法可以查看官方文档https://springdoc.org/