从现有 Java 代码生成 Swagger 文档?

Generating Swagger documentation from existing Java code?

有没有办法从现有的 Java 代码生成 swagger 文档(YAML 格式)?

我目前正在使用YAML文件生成Java API代码,但我很感兴趣是否可以实现反向过程。也就是说,如果我可以从现有的 Java 代码生成一个 YAML 文件?

从现有代码生成 swagger 定义的过程很简单。

添加如下依赖(我刚好用的是Maven):

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.9.2</version>
</dependency>

这会在您的应用程序中添加一个 swagger-ui,可以通过

访问
http://<host>:<port>/swagger-ui.html

点击 API 文档(/v2/api-docs) link and you will get a JSON file that can be easily converted to YAML file using http://editor.swagger.io(编辑 -> 转换为 YAML)