springdoc-openapi-ui throws java.lang.ClassNotFoundException when swagger-parser is on the classpath

817 Views Asked by At

In a project which I currently try to upgrade to Spring Boot 3.x I'm using "springdoc-openapi-starter-webmvc-ui" and I also need "swagger-parser" for my business logic (I thought those two have no dependency on each other, they are used for two different use cases).

Somehow those two do not work together in Spring Boot 3 anymore. They worked fine in Spring Boot 2.x

I was able to reduce the problem into a very simple test setup:

  1. Create a new Spring Boot project using the Spring Initializr. Using all defaults: Boot 3.1.1, Java 17, Gradle. Only add one dependency: "Spring Web"
  2. Adapt build.gradle and add the springdoc and swagger-parser dependency
  3. Build and start the app
  4. Try to open http://localhost:8080/swagger-ui.html
  5. The swagger UI loads but shows an error "Fetch error: response status is 500 /v3/api-docs"

This is exception which is thrown when swagger UI tries to load the REST controllers:

java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlElement
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445) ~[na:na]
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587) ~[na:na]
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:149) ~[demo-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na]
        at io.swagger.v3.core.jackson.SwaggerAnnotationIntrospector.hasRequiredMarker(SwaggerAnnotationIntrospector.java:29) ~[swagger-core-2.2.14.jar!/:2.2.14]
        at org.springdoc.core.configuration.SpringDocRequiredModule$RespectSchemaRequiredAnnotationIntrospector.hasRequiredMarker(SpringDocRequiredModule.java:60) ~[springdoc-openapi-starter-common-2.1.0.jar!/:2.1.0]
        ...

For the sake of completness, here is the exact dependenies as adapted in the build.gradle file (no other changes were made to the whole project as it was created by Spring Initializr

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'

    implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'
    implementation 'io.swagger.parser.v3:swagger-parser:2.1.16'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

If I remove the dependency line with swagger-parser (i.e. only keep the springdoc-openapi-starter-webmvc-ui dependency) everything works fine. Swagger UI opens up as normale, no errors no exceptions.

It looks to me as if somehow the presence of the swagger parser cause springdoc-openapi to try to call it. Which makes no sense to me, why does it try to use swagger-parser when it just work fine without it? Is there maybe a way to "hide" swagger-parser from springdoc-openapi?

0

There are 0 best solutions below