如何配置springdoc默认以YAML格式展示Swagger API文档?
实现SpringDoc Swagger默认展示YAML格式API文档
完全可以实现这个需求,下面提供两种常用的配置方式:
方法一:通过配置文件直接指定
在Spring Boot的配置文件(application.properties 或 application.yml)中添加配置,将Swagger UI默认加载的文档地址指向YAML格式的API文档:
使用application.properties
springdoc.swagger-ui.url=/v3/api-docs.yaml
使用application.yml
springdoc: swagger-ui: url: /v3/api-docs.yaml
配置完成后,启动项目访问Swagger UI页面,默认展示的就是YAML格式的API文档。
方法二:编程式配置
如果需要更灵活的控制,可以通过自定义配置类修改Swagger UI的默认配置:
import org.springdoc.core.configuration.SwaggerUiConfigurer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SwaggerUiCustomConfig { @Bean public SwaggerUiConfigurer swaggerUiConfigurer() { return swaggerUiBuilder -> swaggerUiBuilder.url("/v3/api-docs.yaml"); } }
配置后,Swagger UI会默认加载YAML格式的API文档,同时你依然可以通过页面上的下拉菜单切换回JSON格式的文档。
内容的提问来源于stack exchange,提问作者user1593165
相关产品推荐
相关产品推荐

