简体   繁体   English

如何从现有的 Spring REST API 生成 OpenAPI 3.0 YAML 文件?

[英]How to generate OpenAPI 3.0 YAML file from existing Spring REST API?

I have an existing Spring REST API for which I want to generate the OpenAPI 3.0 YAML file and not Swagger 2.0 JSON/YAML?我有一个现有的 Spring REST API,我想为其生成 OpenAPI 3.0 YAML 文件而不是 Swagger 2.0 JSON/YAML?

Since as of now, SpringFox does not support YAML generation.从现在开始,SpringFox 不支持 YAML 生成。 It generates JSON with Swagger 2.0 (which follows OPEN API 3.0 spec).它使用 Swagger 2.0(遵循 OPEN API 3.0 规范)生成 JSON。

Also, there is https://github.com/openapi-tools/swagger-maven-plugin but it does not seem to support Spring Rest.此外,还有https://github.com/openapi-tools/swagger-maven-plugin但它似乎不支持 Spring Rest。

I tried the Kongchen spring-maven-plugin which is able to generate the YAML file but with Swagger 2.0 definition and not OPEN API 3.0 like :我尝试了 Kongchen spring-maven-plugin,它能够生成 YAML 文件,但使用 Swagger 2.0 定义而不是 OPEN API 3.0,例如:

swagger: "2.0"
info:
  description: "Test rest project"
  version: "1.0"
  title: "Some desc"
  termsOfService: "http://swagger.io/terms/"
  contact:
    name: "Rest Support"
    url: "http://www.swagger.io/support"
    email: "support@swagger.io"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "example.com"
basePath: "/api/"

So my question is how can I generate the OPEN API YAML file like :所以我的问题是如何生成 OPEN API YAML 文件,例如:

openapi: 3.0.0
info:
  description: Some desc
  version: "1.0"
  title: Test rest project
  termsOfService: http://swagger.io/terms/
  contact:
    name: Rest Support
    url: http://www.swagger.io/support
    email: support@swagger.io
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html

I am currently using swagger-maven-plugin to generate YAML file with Swagger 2.0 definition and converting it to Open API 3.0 definition using swagger2openapi at https://mermade.org.uk/openapi-converter我目前正在使用swagger-maven-plugin生成带有 Swagger 2.0 定义的 YAML 文件,并使用https://mermade.org.uk/openapi-converter 上的swagger2openapi将其转换为 Open API 3.0 定义

Question 1:问题 1:
Can spring-maven-plugin capture io.swagger.v3.oas.annotations to generate the YAML ? spring-maven-plugin 可以捕获io.swagger.v3.oas.annotations以生成 YAML 吗?

Question 2:问题2:
What is the best way to generate the YAML with OPEN API definitions in a Spring MVC Project?在 Spring MVC 项目中使用 OPEN API 定义生成 YAML 的最佳方法是什么?

Question 3:问题 3:
Can io.swagger.v3.oas be used with Spring projects or it is only for JAX-RS projects? io.swagger.v3.oas可以用于 Spring 项目还是仅用于 JAX-RS 项目?

We have used lately springdoc-openapi java library.我们最近使用了springdoc-openapi java 库。 It helps automating the generation of API documentation using spring boot projects.它有助于使用 Spring Boot 项目自动生成 API 文档。

It automatically deploys swagger-ui to a spring-boot application Documentation will be available in HTML format, using the official [swagger-ui jars]:它会自动将swagger-ui部署到 spring-boot 应用程序 文档将以 HTML 格式提供,使用官方 [swagger-ui jars]:

The Swagger UI page should then be available at http://server:port/context-path/swagger-ui.html and the OpenAPI description will be available at the following url for json format: http://server:port/context-path/v3/api-docs Swagger UI 页面应该在http://server:port/context-path/swagger-ui.html上可用,OpenAPI 描述将在以下 json 格式的 url 上可用: http://server:port/context-path/v3/api-docs

  • server: The server name or IP服务器:服务器名称或 IP
  • port: The server port端口:服务器端口
  • context-path: The context path of the application上下文路径:应用程序的上下文路径

Documentation can be available in yaml format as well, on the following path: /v3/api-docs.yml .文档也可以 yaml 格式提供,位于以下路径: /v3/api-docs.yml Add the library to the list of your project dependencies (No additional configuration is needed)将库添加到您的项目依赖项列表中(无需额外配置)

 <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-ui</artifactId>
      <version>1.2.3</version>
  </dependency>

I was missing some library for this for longer time.我为此失去了一些图书馆很长一段时间。 Finally, decided to implement my own generator https://github.com/jrcodeza/spring-openapi maybe you can check it out too.最后,决定实现我自己的生成器https://github.com/jrcodeza/spring-openapi也许你也可以看看。 It's based on reflection and supports javax and spring annotations.它基于反射,支持javax和spring注解。 It also generates inheritance model (with discriminators) based on Jackson annotations.它还基于 Jackson 注释生成继承模型(带有鉴别器)。 Besides you can define your own interceptors if you want to alter generation process (eg when you have your own annotations and need to adjust generated sections of schema).此外,如果您想更改生成过程(例如,当您有自己的注释并需要调整模式的生成部分时),您可以定义自己的拦截器。 You can use it in runtime mode or as a maven plugin.您可以在运行时模式下或作为 maven 插件使用它。 There is also OpenAPI3 to java client generator, which generates the model.还有 OpenAPI3 到 java 客户端生成器,它生成模型。 Again it generates also Javax annotations and Jackson annotations for correct inheritance.同样,它还会生成 Javax 注释和 Jackson 注释以进行正确的继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM