首页 > 解决方案 > Swagger UI - 加载自定义 file.yaml/json 而不是默认配置

问题描述

我正在开发一个运行完美的 SpringBoot REST 项目。我正在尝试在项目中实现OpenApi-ui。默认情况下它工作正常,但我想使用我自己的yaml/json信息文件而不是默认信息。

我一直在关注常见问题解答 SpringDoc文档,但没有什么对我有用。它在 UI 中抛出FAILED TO LOAD API DEFINITION : Fetch error undefined /open-api.yaml。我的配置中是否缺少某些内容?提前致谢。

执行

implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.5.9'

应用配置(路由 yaml = src/main/resources)

springdoc:api-docs:enabled: false
         swagger-ui:url: /open-api.yaml

配置

    @Bean
public OpenAPI customOpenAPI() {
    return new OpenAPI()
            .components(new Components().addSecuritySchemes("basicScheme",
                    new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic")))
            .info(new Info().title("MyApp").version("1.0")
                    .license(new License().name("Apache 2.0").url("http://springdoc.org")));
}

@Bean
public SpringDocConfiguration springDocConfiguration(){
    return new SpringDocConfiguration();
}
@Bean
public SpringDocConfigProperties springDocConfigProperties() {
    return new SpringDocConfigProperties();
}

yaml 文件

    openapi: 3.0.3
info:
  title: MyApp
  description: MyApp Description
  version: 1.0.0
servers:
  - url: http://localhost:8080
    description: Local server
{...more}

访问 OpenApi UI 的 URL http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config

OpenApi 用户界面图像

标签: spring-bootswaggerswagger-uiopenapispringdoc-openapi-ui

解决方案


如果有人在寻找类似的东西,我们最终创建了一个新类,扩展SwaggerIndexPageTransformer并由SwaggerIndexTransformer实现,这导致我们使用 @override 方法来更改 url。

您可以关注 > https://github.com/springdoc/springdoc-openapi/issues/763


推荐阅读