Hello
I'm trying to document my REST API using OpenAPI annotations. Despite annotating my controller method with @OpenApi, I'm encountering a "no api definition provided" error when trying to access the generated documentation via Swagger UI or ReDoc.
Here's the relevant portion of my Kotlin code where I set up Javalin, configure the OpenAPI plugin, and define a simple UserController that I took from an example
package org.example
import io.javalin.Javalin
import io.javalin.apibuilder.ApiBuilder.*
import io.javalin.http.Context
import io.javalin.openapi.*
import io.javalin.openapi.plugin.OpenApiPlugin
import io.javalin.openapi.plugin.redoc.ReDocPlugin
import io.javalin.openapi.plugin.swagger.SwaggerPlugin
import java.util.concurrent.atomic.AtomicInteger
data class User(
val id: Int,
val name: String,
val email: String
)
object UserService {
private val users = hashMapOf(
0 to User(id = 0, name = "Alice", email = "alice@alice.kt"),
1 to User(id = 1, name = "Bob", email = "bob@bob.kt"),
2 to User(id = 2, name = "Carol", email = "carol@carol.kt"),
3 to User(id = 3, name = "Dave", email = "dave@dave.kt")
)
private var lastId: AtomicInteger = AtomicInteger(users.size - 1)
fun getAll() = users.values
}
fun getConfiguredOpenApiPlugin(): OpenApiPlugin {
return OpenApiPlugin { pluginConfig ->
pluginConfig.withDefinitionConfiguration { _, definition ->
definition.withInfo { info: OpenApiInfo ->
info.title = "sentinel"
info.version = "1.0.0"
info.description = "sentinel"
}
}
pluginConfig.documentationPath = "/swagger-docs"
}
}
object UserController {
@OpenApi(
summary = "Get all users",
operationId = "getAllUsers",
tags = ["User"],
responses = [OpenApiResponse("200", [OpenApiContent(Array<User>::class)])],
path = "/users",
methods = [HttpMethod.GET]
)
fun getAll(ctx: Context) {
ctx.json(UserService.getAll())
}
}
fun main() {
Javalin.create { config ->
config.registerPlugin(OpenApiPlugin { pluginConfig ->
pluginConfig.withDefinitionConfiguration { version, definition ->
definition.withInfo { info: OpenApiInfo ->
info.title = "Javalin OpenAPI example"
}
}
})
config.registerPlugin(SwaggerPlugin())
config.registerPlugin(ReDocPlugin())
config.router.apiBuilder {
path("users") {
get(UserController::getAll);
}
}
}.start(7001)
println("Check out ReDoc docs at http://localhost:7001/redoc")
println("Check out Swagger UI docs at http://localhost:7001/swagger")
}
Hey, you're using Kotlin with Java's annotation processor and that's why you don't get any results. You should use kapt, as far as I see it is supported Maven:
Hello I'm trying to document my REST API using OpenAPI annotations. Despite annotating my controller method with @OpenApi, I'm encountering a "no api definition provided" error when trying to access the generated documentation via Swagger UI or ReDoc.
Here's the relevant portion of my Kotlin code where I set up Javalin, configure the OpenAPI plugin, and define a simple UserController that I took from an example
and the pom
But it seems not detected my definition