我正在关注 openapi 的 micronaut 文档 https://micronaut-projects.github.io/micronaut-openapi/latest/guide/
并且没有生成 UI,每次我尝试访问 /swagger
、/swagger-ui
时都会收到此错误:
{
"message": "Not Found",
"_links": {
"self": {
"href": "/swagger",
"templated": false
}
},
"_embedded": {
"errors": [
{
"message": "Page Not Found"
}
]
}
}
依赖项已安装:
implementation("io.swagger.core.v3:swagger-annotations")
annotationProcessor("io.micronaut.openapi:micronaut-openapi:4.5.2")
application.yml
上的路由器已定义:
micronaut:
application:
name: myapp
router:
static-resources:
default:
enabled: true
swagger:
enabled: true
paths: classpath:META-INF/swagger
mapping: /swagger/**
并在根文件夹中创建文件openapi.properties
:
swagger-ui.enabled=true
micronaut.openapi.views.spec=apidoc.enabled=true,swagger-ui.enabled=true,swagger-ui.theme=flattop
micronaut.openapi.expand.api.version=v0.1
micronaut.openapi.expand.openapi.description=myapp
根据文档,应生成 .yml
文件;
For Kotlin build/tmp/kapt3/classes/main/META-INF/swagger/myapp-0.1.yml
在我的 Controller 上,我也有 Operation
和 ApiResponses
注释,但是当我运行应用程序时,不会生成文件 yml
,如何在我的 micronaut 项目上正确启用 swagger?
Code sample: https://github.com/rafa-acioly/micronaut
最佳答案
您在 application.yml
中仅定义了 swagger
。您还应该添加具有类似定义的 swagger-ui
部分:
micronaut:
application:
name: openapitest
router:
static-resources:
swagger:
paths: classpath:META-INF/swagger
mapping: /swagger/**
swagger-ui:
paths: classpath:META-INF/swagger/views/swagger-ui
mapping: /swagger-ui/**
此外,我使用了默认的 openapi.properties
文件:
swagger-ui.enabled=true
redoc.enabled=false
rapidoc.enabled=false
rapidoc.bg-color=#14191f
rapidoc.text-color=#aec2e0
rapidoc.sort-endpoints-by=method
您是否尝试过使用 gradlew
来清理
并构建
您的项目?
我创建了一个示例 repository在 GitHub 上,您可以查看它。
========更新========
好的,我发现哪里不对了!您正在使用注释处理
:
annotationProcessor("io.micronaut.openapi:micronaut-openapi:4.5.2")
但是如果您使用 Kotlin,则应该使用 kapt
- Kotlin 注解处理工具。这里有几篇文章 - link1 , link2 .
所以你应该更改为:
kapt("io.micronaut.openapi:micronaut-openapi:4.5.2")
关于kotlin - 如何在 micronaut 上启用 swagger UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74159094/