簡體   English   中英

Spring-boot swagger 在 io.swagger.models.parameters.Parameter 上拋出 java.lang.ClassNotFoundException

[英]Spring-boot swagger throws java.lang.ClassNotFoundException on io.swagger.models.parameters.Parameter

我正在使用以下 spring-boot 依賴項來為我的Spring-boot應用程序生成一個 swagger 文件:

<dependency>
    <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger-fox.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>swagger-annotations</artifactId>
                    <groupId>io.swagger</groupId>
            </exclusion>
            <exclusion>
                <artifactId>swagger-models</artifactId>
                <groupId>io.swagger</groupId>
            </exclusion>
        </exclusions>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger-fox.version}</version>
</dependency>

<properties>
        <swagger.version>1.6.0</swagger.version>
        <java.version>11</java.version>
        <dockerfile-maven-version>1.4.3</dockerfile-maven-version>
        <swagger-fox.version>2.9.2</swagger-fox.version>
</properties>

這是我在我的應用程序中包含的 swagger 配置 class:


@SpringBootApplication
@EnableSwagger2
public class SpringFoxConfig {


    private static final String AUTHOR_MAIL = "xxx@xxx.com";

    private static final String API_TITLE = "DEMO  Service REST API";

    private static final String API_DESCRIPTION = "demo";

    private static final String API_BASE_PACKAGE = "demo_package";


    private ApiInfo getApiInfo() {
        return new ApiInfo(API_TITLE, API_DESCRIPTION, "V1", "", AUTHOR_MAIL,
                "","");
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(getApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(API_BASE_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

盡管如此,當我運行我的應用程序時,我收到以下錯誤:


java.lang.NoClassDefFoundError: io/swagger/models/parameters/Parameter
    at java.base/java.lang.Class.getDeclaredConstructors0(Native Method) ~[na:na]
    at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3137) ~[na:na]
    at java.base/java.lang.Class.getDeclaredConstructors(Class.java:2357) ~[na:na]
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindConstructorProvider.findConstructorBindingAnnotatedConstructor(ConfigurationPropertiesBindConstructorProvider.java:62) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindConstructorProvider.getBindConstructor(ConfigurationPropertiesBindConstructorProvider.java:48) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.context.properties.ConfigurationPropertiesBean$BindMethod.forType(ConfigurationPropertiesBean.java:311) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.validate(ConfigurationPropertiesBeanDefinitionValidator.java:63) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.postProcessBeanFactory(ConfigurationPropertiesBeanDefinitionValidator.java:45) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:174) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at demo.Application.main(Application.java:10) ~[classes/:na]
Caused by: java.lang.ClassNotFoundException: io.swagger.models.parameters.Parameter
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
    ... 19 common frames omitted

請注意,我還收到一條警告,指出ApiInfo方法已被棄用,並懷疑錯誤與此有關,但我不確定。 誰能幫我弄清楚如何解決這個問題 go? 提前致謝

接口參數來自swagger-models maven 依賴項。

要解決此問題,只需從 POM 文件中刪除相應的排除項:

<exclusion>
  <artifactId>swagger-models</artifactId>
  <groupId>io.swagger</groupId>
</exclusion>

Swagger 在 Spring Boot 2.2.0 及以上版本中無法正常工作。 您可以交替使用

“springdoc-openapi-ui”

教程可在www.baeldung.com/spring-rest-openapi-documentation

<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui -->
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.2.30</version>
</dependency>

當我將版本從 1.6.9 降級到 1.6.4 時,我可以解決我的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM