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?
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.
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.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With