Search code examples
javamavenswaggerdropwizard

How to get swagger.json using Dropwizard in Java


I am using Dropwizard to create my service and REST API, and I added dropwizard-swagger to my project so I can easily access this API on my UI. I am creating a CLI tool that will generate Typescript models of my API on the UI side using openapi-generator:

openapi-generator generate -g typescript-fetch -i http://localhost:8080/swagger.json -o ~/Desktop/test

However, I need the swagger.json definitions, and I am currently getting these from a GET request of the path http://localhost:8080/swagger.json. I want to programatically get the swagger.json (how to get the swagger spec without starting a server) through my CLI Java project. I was looking at ways to programmatically generate the swagger.json and I tried using the swagger-maven-plugin:

            <plugin>
            <groupId>com.github.kongchen</groupId>
            <artifactId>swagger-maven-plugin</artifactId>
            <version>3.1.4</version>
            <configuration>
                <apiSources>
                    <apiSource>
                        <springmvc>false</springmvc>
                        <locations>
                            <location>me.pablo.api.LauraPortraitsRestMethods</location>
                        </locations>
                        <schemes>http,https</schemes>
                        <host>http://localhost:8080/</host>
                        <basePath>/swagger</basePath>
                        <info>
                            <title>My project</title>
                            <version>v1</version>
                            <description>Do this late</description>
                            <termsOfService>
                                http://www.github.com/kongchen/swagger-maven-plugin
                            </termsOfService>
                            <contact>
                                <email>[email protected]</email>
                                <name>Kong Chen</name>
                                <url>http://kongch.com</url>
                            </contact>
                            <license>
                                <url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
                                <name>Apache 2.0</name>
                            </license>
                        </info>
                        <templatePath>${basedir}/generated-sources/strapdown.html.hbs</templatePath>
                        <outputPath>${basedir}/generated-sources/document.html</outputPath>
                        <swaggerDirectory>${basedir}/generated-sources/</swaggerDirectory>
                    </apiSource>
                </apiSources>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

And the class of the Rest methods

package me.pabloestrada.api;

import com.google.inject.Inject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/laura")
@Api(value = "/laura")
public class LauraPortraitsRestMethods
{
    private LauraPortraitsService delegate;
@Inject
public LauraPortraitsRestMethods(final LauraPortraitsService delegate) {
    this.delegate = delegate;
}

@GET
@ApiOperation(value = "Getting developer name")
@Path("/dev")
public String getDeveloperName() {
    return delegate.getDeveloperName();
}
}

When I run mvn clean compile, I don't see anything being generated. I don't see any output from the swagger-maven-plugin. Does anyone have any suggestions to make this work or a different approach to programmatically get the swagger.json?


Solution

  • In case someone is still looking add this to your maven build plugins:

    <plugin>
        <groupId>com.github.kongchen</groupId>
        <artifactId>swagger-maven-plugin</artifactId>
                    <version>3.1.8</version>
                    <configuration>
                        <apiSources>
                            <apiSource>
                                <springmvc>false</springmvc>
                                <locations>
                                    <location>me.pabloestrada.api.*</location>
                                </locations>
                                <schemes>http,https</schemes>
                                <outputFormats>json</outputFormats>
                                <basePath>/</basePath>
                                <host>127.0.0.1:8000</host>
                                <info>
                                    <title>APIs</title>
                                    <version>1.0.0</version>
                                    <description>APIs</description>
                                    <contact>
                                        <email>[email protected]</email>
                                        <name>Support</name>
                                    </contact>
                                    <license>
                                        <url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
                                        <name>Apache 2.0</name>
                                    </license>
                                </info>
                                <swaggerDirectory>swagger</swaggerDirectory>
                                <swaggerFileName>swaggerspec</swaggerFileName>
                            </apiSource>
                        </apiSources>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>compile</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    

    It'll create a swagger directory and a json file called swaggerspec.json in that directory containing your swagger spec.