Getting Started
Add Dependency
To generate OpenAPI specifications, you need to include the ktor-openapi
artifact in the build script.
All artifacts are published to Maven Central.
implementation("io.github.smiley4:ktor-openapi:$version")
implementation 'io.github.smiley4:ktor-openapi:$version'
<dependency>
<groupId>io.github.smiley4</groupId>
<artifactId>ktor-openapi</artifactId>
<version>${version}</version>
</dependency>
Ktor Compatibility and Previous Versions
This project as been split into multiple projects starting with version 5.0.Versions up to 5.0 are called ktor-swagger-ui
instead of ktor-openapi
.
Ktor | Plugin Version | Project Name |
---|---|---|
2.x | up to 3.x | ktor-swagger-ui |
3.x | 4.x | ktor-swagger-ui |
3.x | 5.x | ktor-openapi |
Install OpenAPI
install(OpenApi) { //(1)!
//...(2)
}
- Install the "OpenAPI" plugin to the application.
- Add additional plugin configuration here.
Exposing OpenAPI Specification
routing {
route("api.json") { //(1)!
openApi() //(2)!
}
}
- Create a new route to expose the OpenAPI specification file at
api.json
. - Expose the OpenAPI specification.
Documenting Routes
import io.github.smiley4.ktoropenapi.get //(1)!
get("hello", { //(2)!
description = "A Hello-World route" //(3)!
response {
HttpStatusCode.OK to { //(4)!
description = "A success response"
body<String>() //(5)!
}
}
//...
}) {
call.respondText("Hello World!") //(6)!
}
- Replace
io.ktor.server.routing.get
withio.github.smiley4.ktoropenapi.get
. Same for other http methods. - Enrich
/hello
route with additional information. - Add a description to the route.
- Document the different possible responses.
- Specify the response body type. The schema for the type is generated automatically.
- Handle requests as usual.