How to generate swagger.json using gradle?

tbsalling picture tbsalling · Sep 21, 2016 · Viewed 17.5k times · Source

I want to use swagger-codegen to generate REST clients and possibly static HTML documentation.

However, swagger-codegen needs swagger.json for input.

I am aware, that I can get this from a running REST server equipped with Swagger.

But is there a way to obtain swagger.json directly from my Java code - i.e. to generate it with gradle from the source code - without the need to run the application in a web container, and pointing curl or a browser to it?

Answer

Lachezar Balev picture Lachezar Balev · May 23, 2017

This is a bit old but I was wondering exactly the same... In short I've started the research with:

  • A sample Spring Boot app exposing minimalistic REST API;
  • Swagger annotations on the API methods;
  • Springfox;
  • Gradle as a build tool;

I managed to generate the JSON spec as a build artifact using two different approaches:

  1. By using a gradle port of the swagger-maven-plugin of kongchen.
  2. (Not sure if this counts, because it starts a server anyways) By executing an integration test (Spring's mock MVC) which generates the specification. I borrowed the idea from here.

I've summarized my research in a simple project located here. See the Automation section. Code and examples are included.