swagger从2.x升级到3.0过程中的一些坑:PropertySourcedRequestMappingHandlerMapping找不到与getPluginFor方法找不到

引入依赖

        <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的掌握较差。
希望这篇文章对大家有实质性的帮助。

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值