SpringDoc manually adds Schemas/ApiModels method

1. Find the problem

        When using Knife4J, the version was upgraded. The new Knife4J gave up the maintenance of Springfox and instead used SpringDoc as the document specification. When the project was migrated to use springdoc, it was found that the entity class list (swagger models) lacked manually maintained entity classes. Only the entities returned by the controller are displayed in the list. So we started to fix the bug.

2. Solve the problem

        1. Add models according to official documents

  @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. After the addition was completed, it was found that it did not take effect.

                After adding the entity class list when accessing /doc.html, it is found that there are no manually added entity classes.

        3. Start searching according to the call chain

                

       Found a configuration call in the AbstractOpenApiResource class

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

         4. Modify the configuration according to the official documentation

springdoc.remove-broken-reference-definitions

true

Boolean.

To disable removal of broken reference definitions.

Disable removal of broken reference definitions

After adding it, restart the project and find that the manually added entity class is already in the list.

3. Save money

        Using springdoc in springboot to add entity classes does not display the solution in the list

Add configuration         in  application.yml 

springdoc:
  remove-broken-reference-definitions: false

Guess you like

Origin blog.csdn.net/qq_36532805/article/details/131984068