maven - Swagger |通过 maven 命令将 YAML 转换为 JSON

标签 maven yaml swagger

有什么方法可以将我的 YAML 转换为 JSON 的依赖项添加到我现有的 pom.xml 文件中? (包括验证以防 YAML 不正确)

我正在寻找可以进行与 http://editor.swagger.io/ 上相同的转换(或类似转换)的东西(点击文件->转换为JSON时)

但是我找不到任何可以做这样事情的maven依赖

感谢提前!

最佳答案

Swagger Codegen Maven plugin可以将 OpenAPI/Swagger 定义从 YAML 转换为 JSON,反之亦然。为 swagger: '2.0' 定义使用插件版本 2.x,为 openapi: 3.0.0 定义使用 v.3.x。

该插件自动解析外部 $ref 引用并生成单个输出文件。

OpenAPI 2.0/Codegen 2.x 示例:

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.4.8</version>
    <executions>
        <execution>
            <id>convert</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>https://petstore.swagger.io/v2/swagger.yaml</inputSpec>

                <!-- Output directory, relative to the project directory. Default is ${project.build.directory}/generated-sources/swagger -->
                <output>specs</output>

                <!-- Use "swagger" to convert YAML->JSON or "swagger-yaml" to convert JSON->YAML -->
                <language>swagger</language>
                <configOptions>
                    <!-- Default output file name is swagger.json or swagger.yaml -->
                    <outputFile>petstore.json</outputFile>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

OpenAPI 3.0/Codegen 3.x 示例:

<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.11</version>
    <executions>
        <execution>
            <id>convert</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/uspto.yaml</inputSpec>

                <!-- Use "openapi" to convert YAML->JSON or "openapi-yaml" to convert JSON->YAML -->
                <language>openapi</language>

                <!-- Output directory, relative to the project directory. Default is ${project.build.directory}/generated-sources/swagger -->
                <output>specs</output>
                <configOptions>
                    <!-- Default output file name is openapi.json or openapi.yaml -->
                    <outputFile>uspto.json</outputFile>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

关于maven - Swagger |通过 maven 命令将 YAML 转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57778678/

相关文章:

eclipse - 关于从 eclipse 的 tomcat 服务器内部运行 maven 构建的 resteasy 项目的详细说明?

spring - 在tomcat同根路径下部署多个war文件

amazon-web-services - 从 Codecommit 源代码构建 AWS 跨账户管道

java - Spring 中的 Maven 配置文件和应用程序 yml 文件

java - SWAGGER swagger-codegen 配置

java - 将github android源码导入到eclipse

Tomcat 二进制分发 zip 作为 maven 神器

ruby-on-rails - 环境特定的 shoriken 配置

Python-Flask,Swagger,注解生成API文档

Swagger:从枚举中获取一个或多个值