如何使用gradle生成swagger.json? [英] How to generate swagger.json using gradle?

查看:671
本文介绍了如何使用gradle生成swagger.json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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:


  1. 使用 swagger-maven-plugin 的gradle port 。

  2. (不确定这是否重要,因为它无论如何都会启动服务器)通过执行生成规范的集成测试(Spring的模拟MVC)。我从这里借用了这个想法。

  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.

这篇关于如何使用gradle生成swagger.json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆