从 Swagger 文档生成 Yaml 或 Json 文件

2023-12-20

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

EDIT :这可以通过使用 swagger-maven-plugin 来完成,但我没有找到如何做到这一点的示例?


我回复我自己:)。您可以使用以下命令生成客户端和服务器端文档(yaml、json 和 html)swagger-maven-插件 https://github.com/kongchen/swagger-maven-plugin

将其添加到您的 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>[email protected] /cdn-cgi/l/email-protection</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 https://github.com/kongchen/swagger-maven-example

Execute mvn swagger:generateJson文档将在您的项目中生成/generated/swagger/目录。 将其粘贴到此地址:http://editor.swagger.io http://editor.swagger.io

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Swagger 文档生成 Yaml 或 Json 文件 的相关文章

随机推荐