从 Swagger 文档生成 Yaml 或 Json 文件 [英] Generate Yaml or Json file from Swagger documentation
问题描述
我已经使用 swagger-springmvc 注释开发了一些由 swagger 记录的 Rest web 服务.现在,我想使用 swagger-editor 生成客户端 Rest web 服务代码,但 swagger-editor 需要 Yaml 或 Json 文件.你知道是否有办法生成这个文件?提前致谢
I have developpe some Rest web service documented by swagger, using swagger-springmvc annotions. Now, I want to use swagger-editor to generate client side Rest web service code, but swagger-editor require Yaml or Json file. Do you know if there is a way to generate this file ? Thanks at advance
可以通过使用 swagger-mvn-plugin 来完成,但我没有找到如何做到这一点的示例?
EDIT : It's can be done by using swagger-mvn-plugin , but I don't found an example on how to do it ?
推荐答案
我回复我自己:).您可以使用 swagger-maven-plugin<生成客户端和服务器端文档(yaml、json 和 html)/a>
I reply to my self :) . You can generate client and server side documentation (yaml, json and html) by using swagger-maven-plugin
将此添加到您的 pom.xml:
Add this to your 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
You can download *.hbs template at this address: https://github.com/kongchen/swagger-maven-example
执行 mvn swagger:generate
Json 文档将在您的项目 /generated/swagger/
目录中生成.把它放在这个地址上:http://editor.swagger.io
Execute mvn swagger:generate
Json documentation will be generated at your project /generated/swagger/
directory.
Past it on this address :
http://editor.swagger.io
并生成您想要的任何内容(您喜欢的技术中的服务器端或客户端 API)
And generate what ever you want (Server side or Client side API in your preferred technology)
这篇关于从 Swagger 文档生成 Yaml 或 Json 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!