从Swagger文档生成Yaml或Json文件

时间:2015-12-17 17:06:29

标签: json rest yaml swagger swagger-editor

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

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

1 个答案:

答案 0 :(得分:5)

我回复自己:)。您可以使用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)

相关问题