Springboot使用Swagger3.0 版本不兼容的问题记录

1,101 阅读1分钟

1. 问题描述

springboot 引入Swagger 3.0 后启动报错

我使用的版本

  • spring-boot-starter-parent 2.1.5.RELEASE
  • springfox-boot-starter 3.0.0

报错如下:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
[main] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    springfox.documentation.schema.plugins.SchemaPluginsManager.viewProvider(SchemaPluginsManager.java:95)

The following method did not exist:

    org.springframework.plugin.core.PluginRegistry.getPluginFor(Ljava/lang/Object;)Ljava/util/Optional;

The method's class, org.springframework.plugin.core.PluginRegistry, is available from the following locations:

    jar:file:/Users/ml/soft/maven/org/springframework/plugin/spring-plugin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar!/org/springframework/plugin/core/PluginRegistry.class

It was loaded from the following location:

    file:/Users/ml/soft/maven/org/springframework/plugin/spring-plugin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.plugin.core.PluginRegistry

2.问题分析

错误信息的意思是,swagger 要依赖于 spring-plugin-core 中的 getPluginFor 方法,但是该方法在当前 springboot 项目的所依赖的 spring-plugin-core 中不存在。swagger 3.0.0 ,也就是 springfox-boot-starter 3.0.0 需要依赖不低于 2.0.0.RELEASE 版本的 spring-plugin-core,如下图:

image.png

而springboot 2.1.5中引用的版本是 1.2.0.RELEASE

image.png

从而导致项目中使用的版本不对 2.png

3. 解决方案

手动修改 spring-plugin-core的版本

3.png

修改后项目中的版本为2.0.0 可以正常启动

4.png