引入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
代码修改
上图截自于:springfox-boot-starter项目的github地址
可以看到:
- 1、可以移除springfox-swagger2 依赖,因为springfox-boot-starter已经依赖了springfox-swagger2 。同时如果原先还依赖了springfox-swagger-ui,也是可以移除的,原因同上。
- 2、移除@EnableSwagger2 注解
- …
项目启动
初次启动
启动时发现报错信息如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'swagger2ControllerMapping' defined in class path resource [springfox/documentation/swagger2/configuration/Swagger2DocumentationConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'swagger2ControllerMapping' threw exception; nested exception is java.lang.NoClassDefFoundError: springfox/documentation/spring/web/PropertySourcedRequestMappingHandlerMapping
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:657)
也就是没有找到PropertySourcedRequestMappingHandlerMapping 类。
随后发现maven的依赖中:springfox-boot-starter依赖的很多jar包 都因为冲突被忽略掉了(如下图)。
打开springfox-boot-starter的pom文件发现:springfox-swagger2 与springfox-swagger-ui 都依赖的是3.0.0 。
上述问题是因为:父工程的pom文件在做依赖管理时规定了springfox-swagger2 与springfox-swagger-ui 的依赖为原先的2.x 版本。
解决:
注释掉父工程的pom文件依赖管理的springfox-swagger2 与springfox-swagger-ui 或者将两者的版本号升级为3.0.0。
再次启动项目
此处参考了:文章
与初次启动时出现问题的原因答题一致:
springfox-boot-starter依赖的spring-plugin-core 和 spring-plugin-metadata版本都是2.0.0
而从maven中看到的却是1.2.0:
这里我没有在父工程的pom文件管理中看到关于这两个依赖的管理信息,所以此处我采取了文章 这个博主的解决方案,强制将依赖版本升级到2.0。
总结
发现:当父工程规定了一些依赖的版本号,那么子工程不管是直接引用的依赖, 还是引用的依赖引用了的其他依赖,都会按照起作用的。
感觉上述问题还是自身对maven的掌握较差。
希望这篇文章对大家有实质性的帮助。