首页 > 解决方案 > 无法在 Spring REST API 中将 swagger2 版本从 2.7.0 升级到 3.00(常规 Spring 不是 sprint 启动)

问题描述

我的项目在 swagger2 版本 2.7.0 上运行良好。不是 spring boot(仅限 spring mvc)我将升级它 3.0.0 但它给出了插件错误。

java.lang.nosuchMethodError:org.springframework.plugin.core.pluginRegistry.getPluginFor(Ljava/lang/Object;)Ljava/util/Optional;

我的配置如下:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>


Update: As mentioned in https://github.com/springfox/springfox
Step 2 : Add @EnableOpenApi for open API (and @EnableSwagger2WebMvc or @EnableSwagger2WebFlux for older versions)
I added @EnableSwagger2WebMvc in class ,Then it's asking dependency of springfox-swagger2,
as this coming from :
http://springfox.github.io/springfox/javadoc/snapshot/springfox/documentation/swagger2/annotations/EnableSwagger2WebMvc.html 
which is part of springfox-swagger2 dependency

Hence I am confused what to do..
can you please help cleaner approach with steps .
If I don't  use @EnableSwagger2WebMvc/EnableSwagger2
Swagger is not getting loaded

标签: javaspringspring-mvcswagger-2.0spring-rest

解决方案


不确定您使用的是 Spring Boot 还是常规的 Spring 风格,但您是否按照此处发布的迁移说明进行操作?

https://github.com/springfox/springfox

  1. 删除显式依赖springfox-swagger2
  2. 删除任何@EnableSwagger2...注释
  3. 添加springfox-boot-starter(Spring boot) 或springfox-oas(Spring MVC) 依赖项
  4. 检查您是否不依赖已删除的依赖项guava
  5. 如果您正在使用WebMvc但尚未使用@EnableWebMvc注解,请添加此注解(Spring boot)。

推荐阅读