英文:
NoClassDefFoundError with Open API with Spring Boot 3
问题
我升级到Spring Boot 3.0.7,正在尝试再次使我的Open API(swagger)正常工作,使用以下依赖项(根据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>
但是,当我构建我的应用程序时,我收到以下错误:
java.lang.IllegalStateException: Failed to introspect Class [org.springdoc.webmvc.api.OpenApiWebMvcResource] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@1de0aca6]
“Caused By”为:
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
当我查看org.springdoc:springdoc-openapi-webmvc-core:1.7.0
jar中的OpenApiWebMvcResource
时,它确实从javax
而不是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;
那么,这是与openapi-webmvc-core
有关的问题,还是我在连接某些东西时出错了?
英文:
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?
答案1
得分: 1
已升级后出现相同问题。
您只需添加一个依赖项,即springdoc-openapi-starter-webmvc-api,无需额外配置。删除依赖项springdoc-openapi-ui。
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
<version>2.1.0</version>
</dependency>
您无需指定要扫描的包(packagesToScan[])或不需要使用withClassAnnotation(RestController.class)),它会自动处理,因为它会查找@RestController注释并生成文档。
https://springdoc.org/v2/#spring-webmvc-support
英文:
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.
答案2
得分: 1
在我的情况下(Spring Boot 3.0.5 和 Java 17),除了添加以下依赖项之外:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
我还必须添加这个依赖项:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
然后它就可以正常工作了。
英文:
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.
答案3
得分: 0
根据Spring Doc /v2 guide,我只需要引入springdoc-openapi-starter-webmvc-ui
依赖项,而不需要引入springdoc-openapi-ui
。所以当我的pom.xml如下所示:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
...那个错误就消失了,它正常工作了。
请注意,出于某种原因,我还需要引入以下依赖项,否则我会得到一个java.lang.NoClassDefFoundError: jakarta/xml/bind/annotation/XmlRootElement
错误:
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>${jakarta.xml.bind.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>${org.glassfish.jaxb.version}</version>
</dependency>
英文:
Per the Spring Doc /v2 guide, I only needed to bring in the springdoc-openapi-starter-webmvc-ui
dependency, but NOT the springdoc-openapi-ui
. So when my pom.xml looked like:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
...that error went away, and it worked.
Note that for whatever reason, I also needed to pull in these dependencies as well, otherwise I got a java.lang.NoClassDefFoundError: jakarta/xml/bind/annotation/XmlRootElement
:
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>${jakarta.xml.bind.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>${org.glassfish.jaxb.version}</version>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论