Search code examples
spring-bootopenapispringdocspringdoc-openapi-ui

NoClassDefFoundError with Open API with Spring Boot 3


I upgraded to Spring boot 3.0.7 and am trying to get my Open API (swagger) working again, with these dependencies (per springdoc):

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

  <dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.1.0</version>
  </dependency>

...but when I build my app, I get the following error:

java.lang.IllegalStateException: Failed to introspect Class [org.springdoc.webmvc.api.OpenApiWebMvcResource] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@1de0aca6]

...with a "Caused By" of:

java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest

When I look in the OpenApiWebMvcResource that's in the org.springdoc:springdoc-openapi-webmvc-core:1.7.0 jar, it indeed imports from javax instead of jakarta:

package org.springdoc.webmvc.api;
    
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.v3.oas.annotations.Operation;
import java.util.Locale;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
...

So is this an issue with openapi-webmvc-core, or am I wiring something wrong?


Solution

  • had same issue after upgrade.

    you need to add only one dependency which is springdoc-openapi-starter-webmvc-api and No additional configuration is needed. Remove the dependency springdoc-openapi-ui

      <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.1.0</version>
      </dependency>
    

    you don't need to specify packagesToscan[] or no need to use withClassAnnotation(RestController.class)), it'll take care by itself as it look for @RestController Annotation and generate the doc.

    The Swagger UI page will then be available at http://server:port/context-path/swagger-ui.html

    springdoc-openapi Documentation: https://springdoc.org/