Generate Yaml or Json file from Swagger documentation

MK-rou picture MK-rou · Dec 17, 2015 · Viewed 14.3k times · Source

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

EDIT : It's can be done by using swagger-mvn-plugin , but I don't found an example on how to do it ?

Answer

MK-rou picture MK-rou · Jan 6, 2016

I reply to my self :) . You can generate client and server side documentation (yaml, json and html) by using swagger-maven-plugin

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>[email protected]</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> ........

You can download *.hbs template at this address: https://github.com/kongchen/swagger-maven-example

Execute mvn swagger:generate Json documentation will be generated at your project /generated/swagger/ directory. Past it on this address : http://editor.swagger.io

And generate what ever you want (Server side or Client side API in your preferred technology)