Search code examples
spring-bootswaggeropenapi

How to use openapi.yaml file to generate swagger-ui?


I have defined the REST API using an OpenApi specification file. Then I generated Java classes from this specification. Is there a way to use api.yaml to generate swagger-ui.html instead of java classess?

I tried to create an endpoint (with /v3/api-docs path) that reads the file and returns the content of it as a http response. It's working but I have a feeling that there may be a better solution.

Commonly Swagger generates automatically openapi.yaml under the /v3/api-docs endpoint and the Swagerr UI uses it to generate swagger-ui.html. In my case I already have openapi.yaml but I don't know how to tell to Swagger-UI to take my file to generate the html page

Thanks


Solution

  • The openapi-generator has several different generators which can read in a yaml file and output code in your preferred language. Some of the generators include java, html, html2, spring, and javascript. The comprehensive list of generators can be found here.

    The openapi-generator can be ran via CLI, as well as its maven and gradle plugins.

    Since you already have a valid openapi specification file, I'd recommend using the generator to generate a spring server using the spring-boot library and set the documentationProvider to springdoc. This will generate the spring java classes with appropriate springdoc annotations as well as the swagger-ui.html endpoint you are requesting.