12

How to generate JSON file from java files containing Swagger specific annotations, so that Swagger-UI can read it.

Vivek Sinha
  • 1,591
  • 3
  • 15
  • 23
  • Please look at the samples in the [swagger-samples](https://github.com/swagger-api/swagger-samples) repository which demonstrate how this is done. It depends on a number of things, including your framework, etc. – fehguy Feb 22 '16 at 15:59
  • I'm using Java and SpringBoot for framework. – Vivek Sinha Feb 24 '16 at 02:08
  • In swagger-samples repo, the swagger JSON is created manually, I guess. I don't want to do it manually. – Vivek Sinha Feb 24 '16 at 02:44
  • @fehguy could you provide an explicit example of where this is mentioned in your link? – wimnat Jul 14 '16 at 05:04

1 Answers1

3

As you say "I don't want to do it manually", use the Swagger Maven plugin:

<plugin>
    <groupId>com.github.kongchen</groupId>
    <artifactId>swagger-maven-plugin</artifactId>
    <version>3.1.4</version>
    <configuration>
        <apiSources>
            <apiSource>
                <springmvc>true / false</springmvc>
                <locations>
                    <location>com.yourpackage.something</location>
                </locations>
                <host>yourhost.com</host>
                <basePath>/some/path</basePath>
                <info>
                    <title>Your Project Title</title>
                    <version>${project.version}</version>
                    <description>Some nice stuff</description>
                    <termsOfService>...</termsOfService>
                    <contact>
                        <email>someone@somewhere.com</email>
                        <name>Your Name</name>
                        <url>www.where.to.find.you</url>
                    </contact>
                </info>
                <swaggerDirectory>path/to/swagger/output</swaggerDirectory>
                <outputFormats>json</outputFormats>
            </apiSource>
        </apiSources>
    </configuration>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Then you can do mvn compile and it will generate the swagger.json file.

Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
  • It doesn't generate much for me. Seems to detect only `@Api` tags and no `@ApiOperation`, models etc. – ka3ak Jul 23 '21 at 10:36