从现有 Java 代码生成 Swagger 文档? [英] Generating Swagger documentation from existing Java code?
问题描述
有没有办法从现有的 Java 代码生成 swagger 文档(YAML
格式)?
Is there a way to generate swagger documentation (in YAML
format) from existing Java code?
我目前正在使用 YAML 文件生成 Java API 代码,但我对是否可以实现反向过程感兴趣.也就是说,我是否可以从现有的 Java 代码生成一个 YAML 文件?
I am currently using YAML file to generate Java API code, but I am interested whether the reversed process can be achieved. That is if I can generate a YAML file from the existing Java code?
推荐答案
从现有代码生成 YAML 文件的过程很简单.
The process for generating YAML files from existing code is simple.
添加以下依赖项(我碰巧使用Maven):
Add the following dependencies (I happen to use 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
可以使用
This adds a swagger-ui
in your application that can be accessed with
http://<host>:<port>/swagger-ui.html
点击 API 文档 (/v2/api-docs) 链接,您将获得一个可以使用 http://editor.swagger.io 轻松转换为 YAML 文件的 JSON 文件(编辑 -> 转换为 YAML)
Hit the API documentation (/v2/api-docs) link and you will get a JSON file that can be easily converted to YAML file using http://editor.swagger.io (Edit -> Convert to YAML)
这篇关于从现有 Java 代码生成 Swagger 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!