今天在使用Springboot整合swagger2时启动报错:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-03-13 23:34:42.141 ERROR 72552 --- [ main] o.s.boot.SpringApplication : Application run failed
查看运行日志发现报以下错误:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
通过一段时间排查发现注释掉注解@Enableswagger2后springboot正常启动,由此开始查找原因,查找后发现是导入依赖的问题,导入的依赖为:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
解决方法为:
将这两个依赖更改为:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
并在原来加@EnableSwagger2注解的类上加上:
@EnableSwagger2 @EnableWebMvc
这两个注解,发现启动成功
访问的地址变为:http://localhost:8080/swagger-ui/index.html
问题成功解决,如有遇到与本人相同问题的小伙伴可参考。