miha1908
miha1908

Reputation: 177

Generating Swagger documentation from existing Java code?

Is there a way to generate swagger documentation (in YAML format) from existing Java code?

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?

Upvotes: 15

Views: 33020

Answers (1)

Vishrant
Vishrant

Reputation: 16628

The process for generating swagger definition from existing code is simple.

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>

This adds a swagger-ui in your application that can be accessed with

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

enter image description here

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)

Upvotes: 13

Related Questions