有什么方法可以将我的 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/