Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 238
throp Avatar asked Mar 26 '26 18:03

throp


2 Answers

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/

like image 145
Abhishek Kotalwar Avatar answered Mar 29 '26 11:03

Abhishek Kotalwar


In my case (Spring Boot 3.0.5 and Java 17) apart from adding the following

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

I also had to add this dependency

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>

then it worked fine.

like image 21
Seb Avatar answered Mar 29 '26 12:03

Seb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!