都说用新不用旧,但是对于高速迭代的springboot新版本来说集成其他框架经常会出现一些问题。今天使用springboot集成swagger3.0启动就报错:
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
解决办法有三种:
- 将swagger版本降为2.9.2,maven坐标为:
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
- 将springboot的版本降为2.5.7后重新启动项目(听说可以)
并将@EnableSwagger2
注解改为@EnableOpenApi
即可 - 我就想使用swagger3.0还要使用springboot2.6也行:
找了一会终于找到一个解决方案:
后来查了一下资料了解到:springboot2.6更改了请求路径与与SpringMVC路径匹配规则,已经不是原来的AntPathMatcher
了,改为了PathPatternParser
。可能swagger3.0的一些地址还没作出相应的更新所以出错了。只需在配置文件加上一句:
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
如果你的配置文件是yaml:
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
补充:
swagger3.0访问后台地址:http://localhost:8080/swagger-ui/index.html
swagger3.0以下:http://localhost:8080/swagger-ui.html
别忘了swagger3.0要将@EnableSwagger2
注解改为@EnableOpenApi