Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 454
miha1908 Avatar asked Aug 05 '16 13:08

miha1908


People also ask

How do I generate swagger documentation for REST API automatically?

Use Swagger Inspector to quickly generate your OAS-based documentation for existing REST APIs by calling each end point and using the associated response to generate OAS-compliant documentation, or string together a series of calls to generate a full OAS document for multiple API endpoints.

How do I add swagger to an existing Spring project?

To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file. For Gradle users, add the following dependencies in your build. gradle file. Now, add the @EnableSwagger2 annotation in your main Spring Boot application.


1 Answers

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)

like image 85
Vishrant Avatar answered Oct 09 '22 17:10

Vishrant