默认情况下,SpringDoc提供API文档 at /v3/api-docs in json format at /v3/api-docs.yaml in yaml format 第一个在swagger-ui中显示为一个链接。要将第二个显示为link,您需要设置以下spring配置属性 springdoc.swagger-ui.url:/v3/api-docs.yaml
import io.swagger.v3.oas.models.OpenAPI;
import org.springdoc.core.SwaggerUiConfigParameters;
import org.springdoc.core.SwaggerUiConfigParametersBuilder;
import org.springdoc.core.SwaggerUiConfigProperties;
@Configuration
public class SwaggerConfiguration {
@Bean
public OpenAPI openAPI() {
return new OpenAPI();
}
@Bean
public SwaggerUiConfigParameters swaggerUiConfigParameters() {
SwaggerUiConfigProperties properties = new SwaggerUiConfigProperties();
properties.setConfigUrl("/v3/api-docs.yaml"); // Set the YAML URL as the default
return new SwaggerUiConfigParametersBuilder(properties).build();
}
}
2条答案
按热度按时间svmlkihl1#
默认情况下,SpringDoc提供API文档
at /v3/api-docs in json format at /v3/api-docs.yaml in yaml format
第一个在swagger-ui中显示为一个链接。要将第二个显示为link,您需要设置以下spring配置属性
springdoc.swagger-ui.url:/v3/api-docs.yaml
dba5bblo2#
是的,在使用Springdoc时,Swagger可以默认以YAML格式链接API文档。
Springdoc与Swagger UI集成以生成API文档,Swagger UI支持JSON和YAML格式。要将Springdoc配置为使用YAML作为默认格式,您可以按照以下步骤操作:
包括必要的依赖项:确保项目中具有所需的依赖项。你需要springdoc-openapi-ui来集成Swagger UI,需要snakeyaml来支持YAML。
配置Springdoc使用YAML格式:在Sping Boot 应用程序配置中,您可以定义OpenAPI类型的bean并将其配置为生成YAML输出。您可以使用Springdoc提供的YamlOpenAPIResource类来生成YAML文档。
在此配置中,setConfigUrl()用于设置SwaggerUI配置的默认URL。通过将其设置为“/v3/api-docs.yaml”,它告诉SwaggerUI默认以YAML格式加载文档。
生成并访问YAML文档:启动您的应用程序,您可以通过导航到以下URL访问YAML格式的API文档:
猛击
此URL将加载带有YAML格式API文档的Swagger UI。
注意:configUrl参数用于指定Swagger配置文件的URL。在本例中,它指向Springdoc生成的YAML文件。