I have a problem, I have already generated OpenApi Swagger files (swagger.json and swagger.yaml) Is it possible somehow in Ktor (kotlin) import this files and start swagger-ui server on specific route ? I have visited ktor-swagger project but could get how just to add json file to display swagger-ui. Any suggestions ?
Asked
Active
Viewed 3,368 times
3
-
currently, this lib does not support ktor 2.0. And probably maintainer stoped working on it – Victor Orlyk Jul 10 '22 at 03:45
1 Answers
7
Make you json file accessible from static routing
Create "files" directory in resources dir
Use next code to routing static files from "files" dir
routing { static("/static") { resources("files") } //... }
Lets name json file "test.json", then put you it in "files" folder
so now you run the app and see this file by http://localhost:8080/static/test.json
Then we need to install and configure OpenApiGen
Add OpenApiGen dependencies you can find how to do this here https://github.com/papsign/Ktor-OpenAPI-Generator
Then use the following code
install(OpenAPIGen) { serveSwaggerUi = true swaggerUiPath = "/swagger-ui" }
now you can use http://localhost:8080/swagger-ui/index.html?url=/static/test.json URL to see your file through the swagger UI
Also, you can provide your own route to using redirection
get("/api") {
call.respondRedirect("/swagger-ui/index.html?url=/static/test.json", true)
}
this will allow you to use just http://localhost:8080/api

Cube
- 362
- 4
- 8