【问题标题】:Swagger UI with Java9 (spring boot 2.0)使用 Java9 的 Swagger UI (spring boot 2.0)
【发布时间】:2018-05-07 08:57:37
【问题描述】:

我正在尝试使招摇的 UI 与我的服务一起使用。我使用的是spring boot 2.0和Java 9。为swagger添加了以下依赖项

compile('io.springfox:springfox-swagger2:2.8.0')
compile('io.springfox:springfox-swagger-ui:2.8.0')

swagger 已按应有的方式配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

编译良好,但当我使用 BootRun 命令启动应用程序时,由于以下原因失败:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xmlModelPlugin': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [springfox.documentation.schema.XmlModelPlugin] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@4f8e5cde]

Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlType
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    ... 25 common frames omitted

可能是什么问题?不兼容Java9?那我能不能让它工作呢?

【问题讨论】:

    标签: spring spring-boot swagger-ui java-9 swagger-2.0


    【解决方案1】:

    是的,这与 java 9 不兼容。

    我认为你有三个选择:

    进一步阅读 Spring Boot 和 java 9:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-with-Java-9-and-above

    【讨论】:

    • 根据 JDK 9 文档,--add-modules java.xml.bind 是一种临时解决方法,因为 Java SE 中不推荐使用 java.xml.bind 以进行删除。它已在 JDK 11 EA 版本中被删除,请参阅 JEP 320 了解所有详细信息。最好的选择是迁移到 JAXB 的独立版本,API 和实现可以从 Maven Central 下载。
    • @AlanBateman 感谢您的评论。基于此更新答案
    【解决方案2】:

    我不确定这个非常具体的错误,但总的来说 Swagger 2.9.x 似乎与 SpringBoot 2.0.x 有一些不兼容

    一些记录在案的问题: https://github.com/springfox/springfox/issues/1773(主一) https://github.com/springfox/springfox/issues/2501

    有一个 3.0.0-SNAPSHOT 版本,使用这个 Spring Boot 版本可能会更稳定。

    【讨论】:

      【解决方案3】:

      在 maven Central 中找不到 2.9.0 版。可用版本为2.9.1 和2.9.2

      https://mvnrepository.com/artifact/io.springfox/springfox-swagger2

      正如@eis 在从2.8.0 升级到2.9.2 后建议的那样,为我解决了这个问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-01-04
        • 2018-02-12
        • 2018-01-27
        • 2021-05-18
        • 1970-01-01
        • 2018-11-10
        相关资源
        最近更新 更多