从 Swagger 文档生成 Yaml 或 Json 文件

Generate Yaml or Json file from Swagger documentation

我使用 swagger-springmvc 注释开发了一些由 swagger 记录的 Rest web 服务。 现在,我想使用 swagger-editor 生成客户端 Rest web 服务代码,但 swagger-editor 需要 Yaml 或 Json 文件。您知道是否有生成此文件的方法吗? 提前致谢

编辑: 它可以通过使用 swagger-mvn-plugin 来完成,但我没有找到如何做的例子?

我回复我自己 :) 。您可以使用 swagger-maven-plugin

生成客户端和服务器端文档(yaml、json 和 html)

将此添加到您的 pom.xml:

.....
 <plugin>
                <groupId>com.github.kongchen</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <apiSources>
                        <apiSource>
                            <springmvc>true</springmvc>
                            <locations>com.yourcontrollers.package.v1</locations>
                            <schemes>http,https</schemes>
                            <host>localhost:8080</host>
                            <basePath>/api-doc</basePath>
                            <info>
                                <title>Your API name</title>
                                <version>v1</version>
                                <description> description of your API</description>
                                <termsOfService>
                                    http://www.yourterms.com
                                </termsOfService>
                                <contact>
                                    <email>your-email@email.com</email>
                                    <name>Your Name</name>
                                    <url>http://www.contact-url.com</url>
                                </contact>
                                <license>
                                    <url>http://www.licence-url.com</url>
                                    <name>Commercial</name>
                                </license>
                            </info>
                            <!-- Support classpath or file absolute path here.
                            1) classpath e.g: "classpath:/markdown.hbs", "classpath:/templates/hello.html"
                            2) file e.g: "${basedir}/src/main/resources/markdown.hbs",
                                "${basedir}/src/main/resources/template/hello.html" -->
                            <templatePath>${basedir}/templates/strapdown.html.hbs</templatePath>
                            <outputPath>${basedir}/generated/document.html</outputPath>
                            <swaggerDirectory>generated/swagger-ui</swaggerDirectory>
                            <securityDefinitions>
                                <securityDefinition>
                                    <name>basicAuth</name>
                                    <type>basic</type>
                                </securityDefinition>
                            </securityDefinitions>
                        </apiSource>
                    </apiSources>
                </configuration>
            </plugin> ........

您可以在这个地址下载*.hbs模板: https://github.com/kongchen/swagger-maven-example

执行mvn swagger:generate Json 文档将在您的项目 /generated/swagger/ 目录中生成。 把它放在这个地址上: http://editor.swagger.io

并生成您想要的任何内容(服务器端或客户端 API 在您的首选技术中)