How to generate swagger.json

I have done this with a small trick

I have added the following code in the end of my home controller test case

import org.springframework.boot.test.web.client.TestRestTemplate;

public class HomeControllerTest extends .... ...... {

@Autowired
private TestRestTemplate restTemplate;


@Test
public void testHome() throws Exception {
     //.......
     //... my home controller test code 
     //.....

    String swagger = this.restTemplate.getForObject("/v2/api-docs", String.class);

    this.writeFile("spec.json", swagger );
}

public void writeFile(String fileName, String content) {

    File theDir = new File("swagger");

    if (!theDir.exists()) {
        try{
            theDir.mkdir();
        } 
        catch(SecurityException se){ }        
    }

    BufferedWriter bw = null;
    FileWriter fw = null;
    try {
        fw = new FileWriter("swagger/"+fileName);
        bw = new BufferedWriter(fw);
        bw.write(content);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bw != null)
                bw.close();
            if (fw != null)
                fw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

}
}

I don't know this is right way or not But it is working :)

Dependency

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.4.0</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
    </dependency> 

You can get the url with your swagger-ui html page:

enter image description here

GET http://localhost:8080/v2/api-docs?group=App

And actually you can get all the urls with chrome/firefox develop tools network feature.


I'm a little late here, but I just figured out that you can open your browser console and find the URL to the GET request that returns the JSON definition for your Swagger docs. The following technique worked for me when mapping my API to AWS API Gateway.

To do this:

  1. Navigate to your Swagger docs endpoint
  2. Open the browser console
  3. Refresh the page
  4. Navigate to the network tab and filter by XHR requests
  5. Right click on the XHR request that ends in ?format=openapi
  6. You can now just copy and paste that into a new JSON file!

If you use Maven, 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 )