NoClassDefFoundError与Spring Boot 3的Open API。

huangapple go评论81阅读模式
英文:

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):

  &lt;dependency&gt;
    &lt;groupId&gt;org.springdoc&lt;/groupId&gt;
    &lt;artifactId&gt;springdoc-openapi-ui&lt;/artifactId&gt;
    &lt;version&gt;1.7.0&lt;/version&gt;
  &lt;/dependency&gt;

  &lt;dependency&gt;
    &lt;groupId&gt;org.springdoc&lt;/groupId&gt;
    &lt;artifactId&gt;springdoc-openapi-starter-webmvc-ui&lt;/artifactId&gt;
    &lt;version&gt;2.1.0&lt;/version&gt;
  &lt;/dependency&gt;

...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

  &lt;dependency&gt;
    &lt;groupId&gt;org.springdoc&lt;/groupId&gt;
    &lt;artifactId&gt;springdoc-openapi-starter-webmvc-ui&lt;/artifactId&gt;
    &lt;version&gt;2.1.0&lt;/version&gt;
  &lt;/dependency&gt;

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.

https://springdoc.org/v2/#spring-webmvc-support

答案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

    &lt;dependency&gt;
        &lt;groupId&gt;org.springdoc&lt;/groupId&gt;
        &lt;artifactId&gt;springdoc-openapi-starter-webmvc-ui&lt;/artifactId&gt;
        &lt;version&gt;2.1.0&lt;/version&gt;
    &lt;/dependency&gt;

I also had to add this dependency

&lt;dependency&gt;
    &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
    &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
    &lt;version&gt;2.3.1&lt;/version&gt;
&lt;/dependency&gt;

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:

  &lt;dependency&gt;
    &lt;groupId&gt;org.springdoc&lt;/groupId&gt;
    &lt;artifactId&gt;springdoc-openapi-starter-webmvc-ui&lt;/artifactId&gt;
    &lt;version&gt;2.1.0&lt;/version&gt;
  &lt;/dependency&gt;

...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:

&lt;dependency&gt;
    &lt;groupId&gt;jakarta.xml.bind&lt;/groupId&gt;
    &lt;artifactId&gt;jakarta.xml.bind-api&lt;/artifactId&gt;
    &lt;version&gt;${jakarta.xml.bind.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.glassfish.jaxb&lt;/groupId&gt;
    &lt;artifactId&gt;jaxb-runtime&lt;/artifactId&gt;
    &lt;version&gt;${org.glassfish.jaxb.version}&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2023年5月24日 23:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定