如何使用 openapi.yaml 文件生成 Swagger-UI?

huangapple go评论56阅读模式
英文:

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 classes?

我已经使用OpenApi规范文件定义了REST API,然后从该规范生成了Java类。是否有一种方法可以使用api.yaml来生成swagger-ui.html而不是Java类?

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.

我尝试创建了一个端点(使用/v3/api-docs路径),它可以读取文件并将其内容作为HTTP响应返回。它能够工作,但我觉得可能有更好的解决方案。

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

通常,Swagger会在/v3/api-docs端点下自动生成openapi.yaml,然后Swagger UI使用它来生成swagger-ui.html。在我的情况下,我已经有了openapi.yaml,但我不知道如何告诉Swagger-UI使用我的文件来生成HTML页面。

英文:

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

答案1

得分: 1

openapi-generator有多个不同的生成器,可以读取一个yaml文件,并将代码输出为您喜欢的编程语言。其中一些生成器包括javahtmlhtml2springjavascript。完整的生成器列表可以在这里找到。

可以通过CLI运行openapi-generator,还可以使用其maven和gradle插件

由于您已经有一个有效的openapi规范文件,我建议使用生成器生成一个使用spring-boot库并将documentationProvider设置为springdoc的spring服务器。这将生成具有适当的springdoc注解的spring java类,以及您要求的swagger-ui.html端点。

英文:

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.

答案2

得分: 0

Springdoc v2 依赖项添加到 pom.xml 中,生成了一个基于组件扫描的控制器的 Swagger UI 页面,可通过 http://localhost:8080/swagger-ui/index.html 访问,以及其 OpenAPI 描述位于 http://localhost:8080/v3/api-docs

<dependency>
	<groupId>org.springdoc</groupId>
	<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
	<version>2.0.4</version>
</dependency>

请注意,我使用的是 Spring Boot 3.0.2。

英文:

Including Springdoc v2 dependency into pom.xml generated a Swagger UI page based on the component-scanned controllers accessible at http://localhost:8080/swagger-ui/index.html and its OpenAPI description at http://localhost:8080/v3/api-docs.

<dependency>
	<groupId>org.springdoc</groupId>
	<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
	<version>2.0.4</version>
</dependency>

Note I use Spring Boot 3.0.2

huangapple
  • 本文由 发表于 2023年4月13日 15:42:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定