Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

java - How to generate swagger.json using gradle?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...