java springdoc中默认的yaml格式

6ojccjat  于 2023-05-27  发布在  Java
关注(0)|答案(2)|浏览(249)

使用springdoc是否可以让swagger默认链接yaml格式的API文档?
默认情况下,springdoc有swagger链接json格式的API文档。有时候,在默认情况下,最好使用yaml格式的文档。

svmlkihl

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

dba5bblo

dba5bblo2#

是的,在使用Springdoc时,Swagger可以默认以YAML格式链接API文档。
Springdoc与Swagger UI集成以生成API文档,Swagger UI支持JSON和YAML格式。要将Springdoc配置为使用YAML作为默认格式,您可以按照以下步骤操作:
包括必要的依赖项:确保项目中具有所需的依赖项。你需要springdoc-openapi-ui来集成Swagger UI,需要snakeyaml来支持YAML。

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.7.0</version>
</dependency>
<dependency>
    <groupId>org.yaml</groupId>
    <artifactId>snakeyaml</artifactId>
    <version>2.0</version>
</dependency>

配置Springdoc使用YAML格式:在Sping Boot 应用程序配置中,您可以定义OpenAPI类型的bean并将其配置为生成YAML输出。您可以使用Springdoc提供的YamlOpenAPIResource类来生成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();
    }
}

在此配置中,setConfigUrl()用于设置SwaggerUI配置的默认URL。通过将其设置为“/v3/api-docs.yaml”,它告诉SwaggerUI默认以YAML格式加载文档。
生成并访问YAML文档:启动您的应用程序,您可以通过导航到以下URL访问YAML格式的API文档:
猛击

http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs.yaml

此URL将加载带有YAML格式API文档的Swagger UI。
注意:configUrl参数用于指定Swagger配置文件的URL。在本例中,它指向Springdoc生成的YAML文件。

相关问题