如何从OpenAPI资源文件初始化Swagger。

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

How to initialize swagger from openapi resource file

问题

I'm using Kotlin Spring Boot Web MVC应用程序。我已创建了一个名为openapi.yml的api-first文件,其中包含API描述。此外,我还添加了所需的依赖项,如:

  1. Gradle OpenAPI生成插件
    id("org.openapi.generator") version "6.3.0"
  2. OpenAPI
    implementation("org.springdoc:springdoc-openapi-ui:${libs.versions.openapi.get()}")

我已成功使用Gradle生成器从我的文件生成API /模型,但我需要将其制作成Swagger的模板。我已经查看了springdoc的所有设置,似乎只能使用springdoc.swagger-ui.url属性转发生成的文档

是否有一种方式可以手动从openapi.yml文件初始化Swagger?

英文:

I'm using kotlin spring boot web mvc application. I've created an api-first file openapi.yml that contains api description. Also I've added required dependencies, like:

  1. Gradle openapi generator plugin
    id("org.openapi.generator") version "6.3.0"
  2. Openapi implementation("org.springdoc:springdoc-openapi-ui:${libs.versions.openapi.get()}")

I've successfully generated apis / model with gradle generator from my file, but I need to make it as a template for swagger.
I've reviewed all the springfoc's settings and it seems, that I can only forward a generated documentation with a springdoc.swagger-ui.url property.

Is there any way to initialized swagger from a openapi.yml file manually?

答案1

得分: 1

I've managed to find how to resolve this issue.

For springdoc, the right way is described here:
https://springdoc.org/faq.html#_how_can_use_custom_jsonyml_file_instead_of_generated_one

My steps:

  1. Add springdoc settings:
springdoc:
  api-docs:
    enabled: false
  swagger-ui:
    url: /openapi.yaml
  1. As it's written there - it's very important to place yaml file into proper location: src/main/resources/static
  2. For multi-module projects, api docs can be placed at root projects and then copied into the mentioned above path with a custom gradle task, like:
register<Copy>("copyOpenApiDoc") {
    from(rootProjectOpenapiFolder.resolve("openapi.yaml"))
    into(buildDir.resolve("resources/main/static"))
}

Perhaps not a beautiful solution, but it works.

英文:

I've managed to find how to resolve this issue.

For springdoc the right way is described here:
https://springdoc.org/faq.html#_how_can_use_custom_jsonyml_file_instead_of_generated_one

My steps:

  1. Add springdoc settings:
springdoc:
  api-docs:
    enabled: false
  swagger-ui:
    url: /openapi.yaml
  1. As it's written there - it's very important to place yaml file into proper location: src/main/resources/static
  2. For multi-module projects api docs can be placed at root projects and then copied into mentioned above path with a custom gradle task, like:
register&lt;Copy&gt;(&quot;copyOpenApiDoc&quot;) {
        from(rootProjectOpenapiFolder.resolve(&quot;openapi.yaml&quot;))
        into(buildDir.resolve(&quot;resources/main/static&quot;))
}

Perhaps not a beautiful solution, but it works

huangapple
  • 本文由 发表于 2023年4月10日 19:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976829.html
匿名

发表评论

匿名网友

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

确定