首页 文章

从Swagger文档生成Yaml或Json文件

提问于
浏览
2

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

EDIT : 它's can be done by using swagger-mvn-plugin , but I don'找到了一个如何做的例子?

1 回答

  • 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 将在项目 /generated/swagger/ 目录中生成Json文档 . 过了这个地址:http://editor.swagger.io

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

相关问题