如何使用gradle生成swagger.json? [英] How to generate swagger.json using gradle?
问题描述
我想使用swagger-codegen来生成REST客户端和可能的静态HTML文档。
I want to use swagger-codegen to generate REST clients and possibly static HTML documentation.
但是,swagger-codegen需要swagger.json来输入。
However, swagger-codegen needs swagger.json for input.
我知道,我可以从装有Swagger的正在运行的REST服务器上获得这个。
I am aware, that I can get this from a running REST server equipped with Swagger.
但有没有办法直接从我的Java代码获取swagger.json - 即使用源代码中的gradle生成它 - 无需在Web容器中运行应用程序,并指向 curl
或者它的浏览器?
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?
推荐答案
这有点旧但我想知道完全一样......总之我'我开始研究:
This is a bit old but I was wondering exactly the same... In short I've started the research with:
- 一个暴露简约REST API的示例Spring Boot应用程序;
- Swagger关于API方法的注释;
- Springfox;
- Gradle作为构建工具;
- A sample Spring Boot app exposing minimalistic REST API;
- Swagger annotations on the API methods;
- Springfox;
- Gradle as a build tool;
我设法使用两种不同的方法生成JSON规范作为构建工件:
I managed to generate the JSON spec as a build artifact using two different approaches:
- 使用 swagger-maven-plugin 的gradle port 。
- (不确定这是否重要,因为它无论如何都会启动服务器)通过执行生成规范的集成测试(Spring的模拟MVC)。我从这里借用了这个想法。
- By using a gradle port of the swagger-maven-plugin of kongchen.
- (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.
这篇关于如何使用gradle生成swagger.json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!