16

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?

1
  • Could you show some examples as to what you're trying to achieve? Do you mean JavaDoc generation, setters/getters, etc.? Commented Aug 5, 2016 at 13:45

1 Answer 1

13

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)

Sign up to request clarification or add additional context in comments.

9 Comments

Is there any way to automate this process? For example, I want this yaml file to be generated when I build the project.
@GermanFaller yes, I am using these two in my project and it generates the swagger-ui for me but I haven't been able to figure out the way to generate the yaml file automatically as well.
Maybe I will write a post to show how to do this. Have you look on localhost:8080/<MyApp>/<Rest Endpoint>/swagger.yaml ? forget <MyApp>, if you have your app on root.
No, it gives me a 404 error, unfortunately. I am able to access the Swagger UI at http://localhost:9020/swagger-ui.html but when I try to access http://localhost:9020/swagger.yaml I get a 404 error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.