Springfox swagger not working in spring boot 2.2.0
Asked Answered
W

11

36

I want to upgrade spring boot v2.1.9 to 2.2.0. But after the upgrade I am getting some exceptions, which says spring fox is using an older version of spring-plugin-core.

Is there any alternate solution for this or do I need to give up the springfox plugin?

***************************
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.spring.web.plugins.DocumentationPluginsManager.createContextBuilder(DocumentationPluginsManager.java:152)

The following method did not exist:

    org.springframework.plugin.core.PluginRegistry.getPluginFor(Ljava/lang/Object;Lorg/springframework/plugin/core/Plugin;)Lorg/springframework/plugin/core/Plugin;

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

    jar:file:/C:/Users/regosa/.m2/repository/org/springframework/plugin/spring-plugin-core/2.0.0.RELEASE/spring-plugin-core-2.0.0.RELEASE.jar!/org/springframework/plugin/core/PluginRegistry.class

It was loaded from the following location:

    file:/C:/Users/regosa/.m2/repository/org/springframework/plugin/spring-plugin-core/2.0.0.RELEASE/spring-plugin-core-2.0.0.RELEASE.jar


Action:

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

maven dependency tree file : https://drive.google.com/file/d/1gayvvVe_VsB1P2Hi2rcwRw8NdK89qtbq/view

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xxxxx.business.workflow</groupId>
    <artifactId>xxxxx-component-workflow-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>xxxxx-component-workflow-starter</name>
    <description>xxxxx-component-workflow-starter</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
        <jaxb-runtime.version>2.4.0-b180830.0438</jaxb-runtime.version>
        <spring-cloud.version>Greenwich.M1</spring-cloud.version>
       <zeebe-version>0.20.1</zeebe-version>    
        <google-guava.version>27.0.1-jre</google-guava.version>
        <xxxxx.version>1.0</xxxxx.version>
        <swagger.version>2.9.2</swagger.version>
        <jjwt.version>0.9.1</jjwt.version>
        <json.version>20180813</json.version>
        <slf4j-api.version>1.7.25</slf4j-api.version>
        <mysql.version>8.0.11</mysql.version>
        <mongo-java-driver.version>3.10.1</mongo-java-driver.version>
        <commons-io.version>2.6</commons-io.version>
        <commons-lang.version>2.6</commons-lang.version>
        <commons-pool2.version>2.5.0</commons-pool2.version>
        <redis.version>3.1.0</redis.version>
        <velocity.version>1.7</velocity.version>
        <velocity-tools.version>2.0</velocity-tools.version>
        <logstash-logback-encoder.version>5.3</logstash-logback-encoder.version>
        <httpclient.version>4.5.6</httpclient.version>
        <jaxb-runtime.version>2.4.0-b180830.0438</jaxb-runtime.version>
        <env>local</env>
    </properties>   

    <dependencies>
        <!-- Start: Spring Libraries -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        <!-- End: Spring Libraries -->

        <!-- Adding JAXB Runtime since it is not shipped with JDK 9+ -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>${jaxb-runtime.version}</version>
        </dependency>

        <!-- Start: xxxxx Libraries -->
        <dependency>
            <groupId>com.xxxxx</groupId>
            <artifactId>xxxxx-entity</artifactId>
            <version>${xxxxx.version}</version>
        </dependency>
        <dependency>
            <groupId>com.xxxxx</groupId>
            <artifactId>xxxxx-redis</artifactId>
            <version>${xxxxx.version}</version>
        </dependency>
        <dependency>
            <groupId>com.xxxxx</groupId>
            <artifactId>xxxxx-mongo</artifactId>
            <version>${xxxxx.version}</version>
        </dependency>           
        <dependency>
            <groupId>com.xxxxx</groupId>
            <artifactId>xxxxx-util</artifactId>
            <version>${xxxxx.version}</version>
        </dependency>
        <dependency>
            <groupId>com.xxxxx.model</groupId>
            <artifactId>xxxxx-model</artifactId>
            <version>${xxxxx.version}</version>
        </dependency>
        <dependency>
            <groupId>com.xxxxx.service</groupId>
            <artifactId>xxxxx-common-service</artifactId>
            <version>${xxxxx.version}</version>
        </dependency>
        <dependency>
            <groupId>com.xxxxx.service</groupId>
            <artifactId>xxxxx-common-messaging</artifactId>
            <version>${xxxxx.version}</version>
        </dependency>   
        <dependency>
            <groupId>com.xxxxx.service</groupId>
            <artifactId>xxxxx-common-security</artifactId>
            <version>${xxxxx.version}</version>
        </dependency>           
        <!-- End: xxxxx Libraries -->

        <!--  Adding Zeebe client as part of the Spring Startup -->     
        <dependency>
            <groupId>io.zeebe</groupId>
            <artifactId>zeebe-client-java</artifactId>
            <version>${zeebe-version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${google-guava.version}</version>
        </dependency>

        <!-- Logstash Log Encoder -->
        <dependency> 
            <groupId>net.logstash.logback</groupId> 
            <artifactId>logstash-logback-encoder</artifactId> 
            <version>${logstash-logback-encoder.version}</version> 
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>${mongo-java-driver.version}</version>
        </dependency>       

        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>${commons-lang.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>${commons-pool2.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${redis.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>${httpclient.version}</version>
        </dependency>

        <!-- Start: Swagger Libraries -->
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <!-- End: Swagger Libraries -->

        <!-- Start: Spring Boot and Security Test Libraries -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- End: Spring Boot and Security Test Libraries -->

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <finalName>xxxxx-component-workflow-starter</finalName>
        <filters>
            <filter>${env}-build.properties</filter>
        </filters>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <release>${java.version}</release>
                </configuration>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-jar-plugin</artifactId>
              <configuration>
                <archive>
                  <manifestEntries>
                    <Automatic-Module-Name>com.xxxxx.business.workflow.component.starter</Automatic-Module-Name>
                  </manifestEntries>
                </archive>
              </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories> 
</project>
Wende answered 30/10, 2019 at 13:21 Comment(10)
what exception ?Cordellcorder
@ShaileshChandra I updated the question. Actually swagger uses old spring-plugin-core (1.2 V) but spring boot 2.2.0 uses v2.0.Wende
can you share your pom.xml?Logarithmic
@Logarithmic I updated the pom file and also maven dependency treeWende
just updated my sample project from spring boot 2.1.x to 2.o, didn't face any issue, can you share your swagger configCordellcorder
just checked my dependency tree and I guess you are right bout spring-plugin-core version, however, I can't find out what jar is bringing spring-plugin-core-2.0 in your dependency treeCordellcorder
@ShaileshChandra if you use the only swagger with spring boot its not problem, just add spring-data-JPA, spring-data-redis, then the issue will come. because these plugins are using spring-plugin-core 2.0 wherever swagger using 1.2Wende
my application works and pom.xml still resolving to spring-plugin-core 1.2Cordellcorder
Springfox is dead and not supporting spring boot >2.1.9Extenuation
If you are using Spring Security, use the security configuration mentioned on Cannot open Swagger UI in tis Version 3 in my Spring Boot Example page.Tailwind
G
17

Fixed!!!

The issue exists as spring fox libraries internally depend on spring-plugin-core:1.2.0 but it actually requires spring-plugin-core:2.0.0. This dependency correction seems to be missing in the SNAPSHOT version of the swagger libraries for supporting web flux.

We just need to correct Maven dependencies as given below and no code change is required:

  • Maven POM Dependencies fix
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>3.0.0-SNAPSHOT</version>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.plugin</groupId>
      <artifactId>spring-plugin-core</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-spring-webflux</artifactId>
  <version>3.0.0-SNAPSHOT</version>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.plugin</groupId>
      <artifactId>spring-plugin-core</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.springframework.plugin</groupId>
  <artifactId>spring-plugin-core</artifactId>
  <version>2.0.0.RELEASE</version>
</dependency>
  • Swagger Configuration: (Same as before just for reference)
    @Configuration
    @EnableSwagger2WebFlux
    public class SwaggerConfiguration {

        @Bean
        public Docket api(final TypeResolver typeResolver) {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
        }
    }
Gopher answered 25/1, 2020 at 14:34 Comment(1)
If it helps someone I am using springboot with 2.2.8.RELEASE and below swagger version: implementation 'io.springfox:springfox-swagger2:2.7.0' implementation 'io.springfox:springfox-swagger-ui:2.7.0' ---- 2.9.2 version of swagger was not working.Teratoid
S
15

You can use SpringFox 3.0.0-SNAPSHOT with Spring Boot 2.2.X

  • Since it is a SNAPSHOT version, it is not published in the official repository of maven and needs to be pulled from jcenter-snapshots
  • @EnableSwagger2 is removed and changed to @EnableSwagger2WebMvc and @EnableSwagger2WebFlux

Maven

    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0-SNAPSHOT</version>
        </dependency>
    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0-SNAPSHOT</version>
        </dependency>
    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-data-rest</artifactId>
            <version>3.0.0-SNAPSHOT</version>
    </dependency>

Repository

    <repositories>
        <repository>
            <id>jcenter-snapshots</id>
            <name>jcenter</name>
            <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
        </repository>
    </repositories>

Sample Configuration

@EnableSwagger2WebMvc
@Import(SpringDataRestConfiguration.class)
public class SpringFoxConfig {
    //...
}

References

  1. Springfox docs

  2. https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

Note: It seems SpringFox is not dead.

Sanguine answered 15/1, 2020 at 19:29 Comment(1)
3.0.0 was released. SNAPSHOT no longer neededBarye
L
11

Apparently, since SpringFox is abandoned and its last version is no long compatible with Spring Boot 2.2.X, you will have to look for alternatives (see issue reported in Github here).

One possible option is to use OpenAPI docs. For that you need to remove the SpringFoxdependencies and add these:

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.2.15</version>
        </dependency>
         <dependency>
              <groupId>org.springdoc</groupId>
              <artifactId>springdoc-openapi-webmvc-core</artifactId>
              <version>1.2.15</version>
        </dependency>

Also, you will have to remove the Swagger configuration class and provide this:

    @Configuration
    public class OpenApiConfig {

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .components(new Components())
                .info(new Info().title("Contact Application API").description(
                        "This is a sample Spring Boot RESTful service using springdoc-openapi and OpenAPI 3."));
    }
}

In my case, everything seems to work with the exception of the JPA Rest repositories that are not being exposed. I will update this answer with a fix, if I find one.

These are some of the links I was checking to fix this:

Lachrymose answered 2/12, 2019 at 9:1 Comment(8)
who is using isEnabled, why do we need this config?Gwenore
You can ignore it, it was a flag that I used to disable the former Swagger. I have removed from the code sampleLachrymose
still doesnt work for me in spring boot 2.2, it seems swagger-resources are not served from webjars #59293880Gwenore
OpenApi works as a replacement for a monolithic app, but it cannot replace the flexibility of springfox in MSOAImidazole
Why do you say that SpringFox is abandoned? It's quite alive. Just lagging behind a bit. See springfox.github.io/springfoxMasculine
Last version of springfox is from one year ago. It is abandoned. I will migrate to springdoc openapi soon.. Springfox is not following spring updates, you can't use it with lazy configuration and component-indexer.Sewerage
If it helps someone I am using springboot with 2.2.8.RELEASE and below swagger version: implementation 'io.springfox:springfox-swagger2:2.7.0' implementation 'io.springfox:springfox-swagger-ui:2.7.0' And it is working fine.Teratoid
Was facing similar issue with swagger 2.9.2 version.Teratoid
D
5

I upgraded to Springfox Swagger2 to 3.0.0 version and spring plugin code to 2.0.0.RELEASE version. It worked for me.

  • Spring Boot Version - 2.3.9.RELEASE
<dependency>
            <groupId>org.springframework.plugin</groupId>
            <artifactId>spring-plugin-core</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <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>
Discriminate answered 14/4, 2021 at 0:0 Comment(1)
My problem started after include this dependency: <groupId>org.springframework.data</groupId> <artifactId>spring-data-rest-core</artifactId> this solve my problem, thanksReiche
M
2

I am using spring-boot 2.2.2.RELEASE and there was no issue with spring-core and swagger. Below import was the culprit

spring-boot-starter-data-rest
Maryettamaryjane answered 8/4, 2020 at 10:24 Comment(0)
F
1

Remove HATEOS dependency, but add only swagger. It works

//  Using only the swagger as of now, since hateos is not updated the fix for Spring 2.1.X and above
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
Fibrous answered 22/3, 2020 at 4:55 Comment(0)
H
0

I have struggled with this issue while configuring swagger dependencies with the version of 3.0.0 instead of 2.9.2 with Spring-boot 2.3.4-RELEASE version.

Finally, I did it and documented with proper steps to follow. Kindly visit SWAGGER CONFIGURATION page.

Hickie answered 4/11, 2020 at 13:3 Comment(0)
T
0

As nitin-techyplanet has already answered, the spring-plugin-core dependency is resolved with the wrong version 1.2.0.RELEASE. In order to force usage of the correct 2.0.0.RELEASE, you have to EITHER exclude the wrong transitive dependency and specify the correct one, as he/she suggested, OR to pin (!) that dependency version in your pom.xml. This is done in the <dependenciesManagement> block, not in the <dependencies> block. Example:

<spring-plugin-core.version>2.0.0.RELEASE</spring-plugin-core.version>
<springfox.version>3.0.0</springfox.version>
...

<dependencyManagement>
  <dependencies>
    <!-- API Documentation -->
    <!-- Fix wrong resolved `spring-plugin-core` dependency version for springfox -->
    <dependency>
      <groupId>org.springframework.plugin</groupId>
      <artifactId>spring-plugin-core</artifactId>
      <version>${spring-plugin-core.version}</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-boot-starter</artifactId>
      <version>${springfox.version}</version>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <!-- API Documentation -->
  <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
  </dependency>
</dependencies>
Tager answered 14/2, 2021 at 23:14 Comment(0)
G
0

Just had the same issue with springfox-swagger in Spring Boot 2.4.x.

What worked for me was replacing the following dependencies:

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

With the springdoc-openapi-ui:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.5.2</version>
</dependency>

The auto-generated API documentation then can be accessed at: http://localhost:8080/swagger-ui.html

Godman answered 25/3, 2021 at 19:35 Comment(0)
V
0

Using dependencies as below it will resolve the error.

for gradle : implementation "io.springfox:springfox-boot-starter:3.0.0"

for gradle : implementation "io.springfox:springfox-boot-starter:3.0.0"

for maven

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

for maven use below

Vin answered 20/9, 2021 at 11:3 Comment(0)
B
0

I fixed this by following these steps:

1. Downgrade Spring Boot to use 2.4.x version.

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

2. Change version for SpringFox dependencies.

    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>LATEST</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>

Backplate answered 27/12, 2021 at 23:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.