SpringDoc手动添加Schemas/ApiModels方法

1.发现问题

        在使用Knife4J的时候升级了版本,新的Knife4J放弃了Springfox的维护,改为使用SpringDoc作为文档规范,在项目迁移到使用springdoc时,发现实体类列表(swagger models)少了手动维护的实体类,只有controller返回的实体显示在列表里.于是开始修复bug.

2.解决问题

        1.根据官方文档添加models

  @Bean
    open fun openAPI(): OpenAPI {
        val docConfig = voidConfiguration.docConfig
        val info = OpenAPI()
            .info(Info()
                .contact(Contact()
                    .name(Meta.ADMINISTRATOR)
                    .email("xxx")
                    .url("xx")
                )
                .title(docConfig.title)
                .version(Meta.VERSION)
                .description(docConfig.description)
                .termsOfService("xxxx")
                .license(License().name("Apache 2.0").url("xxx"))
            )
        //官方文档从此处添加models/schemas
        val components = Components()
        components.addSchemas("测试资源", Schema<SystemResource>())
        info.components(components)
        return info
    }

        2.添加完成后发现并不生效

                添加以后访问 /doc.html的实体类列表发现没有手动添加的实体类

        3.开始根据调用链查找

                

       在AbstractOpenApiResource类发现一个配置调用

if (springDocConfigProperties.isRemoveBrokenReferenceDefinitions())
				this.removeBrokenReferenceDefinitions(openAPI);

         4.根据官方文档修改配置

springdoc.remove-broken-reference-definitions

true

Boolean.

To disable removal of broken reference definitions.

禁用删除损坏的引用定义

添加完以后重新启动项目,发现手动添加的实体类已经在列表里了

3.省流

        在springboot使用springdoc添加了实体类不在列表里显示解决方案

        在 application.yml 添加配置

springdoc:
  remove-broken-reference-definitions: false

猜你喜欢

转载自blog.csdn.net/qq_36532805/article/details/131984068