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

标签 java spring spring-boot maven swagger

我正在使用以下 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 配置类,我已将其包含在我的应用程序中:


@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 已弃用,并怀疑该错误与其有关,但我不确定。谁能帮我弄清楚如何解决这个问题?提前致谢

最佳答案

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>

关于java - Spring-boot swagger 在 io.swagger.models.parameters.Parameter 上抛出 java.lang.ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60170611/

相关文章:

java - 如何在 UI 上显示进度对话框?

spring-boot - 数据库 "C:/data/sample"未找到,如果 EXISTS=true,所以我们无法自动创建它 - Spring Boot 中的错误

java - 获取 java.lang.NoClassDefFoundError : org/apache/catalina/connector/Connector in wildFly 21. x

java - is CompletedFuture 根据下面的代码会同时触发两个项目吗?

java - @rid 东方数据库中的哪种数据类型?

java - 堆书盒包装

删除 ArrayList 时出现 Java ConcurrentModificationException

java - Jersey 和 Spring 中的全局异常处理?

java - 无法在 Spring Boot Test 1.5 中设置运行时本地服务器端口

java - Spring 安全: How to get the automatically rendered login page code?