从Swagger文档生成Yaml或Json文件

7
我开发了一些使用swagger-springmvc注释记录的Rest web服务。现在,我想使用swagger-editor生成客户端Rest web服务代码,但swagger-editor需要Yaml或Json文件。你知道有没有办法生成这个文件?非常感谢。
编辑: 可以通过使用swagger-mvn-plugin来完成,但我找不到如何做的示例。
1个回答

13

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接