java.lang.IllegalStateException:在使用 spring 引导实现打开 api 时无法读取 class 的元数据

java.lang.IllegalStateException: Unable to read meta-data for class while implementing open api with spring boot

我正在尝试实现 Open API DocOpen API UI 使用Spring 里面 Spring 启动:

我指的是遵循以下指南: https://www.baeldung.com/spring-rest-openapi-documentation

maven中的依赖如下:

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

Spring我使用的引导版本是2.2

我在通过 ./mvnw spring-boot:run

申请 运行 时收到以下元数据相关错误
java.lang.IllegalStateException: Unable to read meta-data for class org.springdoc.core.MultipleOpenApiSupportConfiguration
        at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.getAnnotationMetadata(AutoConfigurationSorter.java:233) ~[spring-boot-autoconfigure-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.getOrder(AutoConfigurationSorter.java:204) ~[spring-boot-autoconfigure-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.access[=15=]0(AutoConfigurationSorter.java:150) ~[spring-boot-autoconfigure-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.autoconfigure.AutoConfigurationSorter.lambda$getInPriorityOrder[=15=](AutoConfigurationSorter.java:63) ~[spring-boot-autoconfigure-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:360) ~[na:na]
        at java.base/java.util.TimSort.sort(TimSort.java:234) ~[na:na]
        at java.base/java.util.Arrays.sort(Arrays.java:1516) ~[na:na]

知道为什么会这样吗?

我遇到了同样的问题,这里是完整的堆栈跟踪:

24-12-2019 15:51:45.312 [restartedMain] ERROR o.s.boot.SpringApplication.reportFailure - Application run failed
java.lang.IllegalStateException: Unable to read meta-data for class org.springdoc.core.MultipleOpenApiSupportConfiguration
    at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.getAnnotationMetadata(AutoConfigurationSorter.java:233)
    at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.getOrder(AutoConfigurationSorter.java:204)
    at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.access[=10=]0(AutoConfigurationSorter.java:150)
    at org.springframework.boot.autoconfigure.AutoConfigurationSorter.lambda$getInPriorityOrder[=10=](AutoConfigurationSorter.java:62)
    at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
    at java.util.TimSort.sort(TimSort.java:234)
    at java.util.Arrays.sort(Arrays.java:1512)
    at java.util.ArrayList.sort(ArrayList.java:1462)
    at org.springframework.boot.autoconfigure.AutoConfigurationSorter.getInPriorityOrder(AutoConfigurationSorter.java:61)
    at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector$AutoConfigurationGroup.sortAutoConfigurations(AutoConfigurationImportSelector.java:430)
    at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector$AutoConfigurationGroup.selectImports(AutoConfigurationImportSelector.java:415)
    at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGrouping.getImports(ConfigurationClassParser.java:878)
    at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.processGroupImports(ConfigurationClassParser.java:801)
    at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorHandler.process(ConfigurationClassParser.java:771)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:185)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
    at com.test.server.Application.main(Application.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.io.FileNotFoundException: class path resource [org/springdoc/core/MultipleOpenApiSupportConfiguration.class] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:51)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103)
    at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:86)
    at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:73)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:81)
    at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.getAnnotationMetadata(AutoConfigurationSorter.java:229)
    ... 32 common frames omitted

问题出在 springdoc-openapi-corespringdoc-openapi-ui 库的不兼容版本上。我通过为两个依赖项设置 1.1.49 版本来修复它:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-core</artifactId>
    <version>1.1.49</version>
</dependency>

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.1.49</version>
</dependency>

springdoc-openapi-ui 具有 springdoc-openapi-core 作为传递依赖。最安全的做法是只定义最深的依赖,并让它拉取其上游库的正确版本,如下所示:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>${springdoc-openapi-ui.version}</version>
</dependency>

换句话说,从您的 POM 依赖项中省略 springdoc-openapi-core,并且仅指定以上内容(当然,将 ${springdoc-openapi-ui.version} 替换为您想要的版本)。