Skip to content

V2 Resources Plugin

Lukas Ruegner edited this page Jun 2, 2024 · 1 revision

Type-safe routes defined with the ktor resources-plugin can be documented exactly the same as normal routes, shown in the following example:

import io.github.smiley4.ktorswaggerui.dsl.resources.get


install(Resources)
install(SwaggerUI)


data class ArticleResponse(
	id: String,
    name: String,
)

@Resource("/articles")
class Articles() {
    @Resource("{id}")
    class Id(val parent: Articles = Articles(), val id: Long)
}

routing {
    get<Articles.Id>({
        description = "Returns the article with the given id"
        request {
            pathParameter<String>("id") {
                description = "The id of the requested article"
            }
        }
        response {
        	HttpStatusCode.OK to {
            	description = "Successful Request"
                body<ArticleResponse>()
        	}
            HttpStatusCode.NotFound to {
            	description = "No article with the requested id exists"
            }
        }
    }) { article ->
        call.respond(ArticleResponse(article.id, "Example Article"))
    }
}
Clone this wiki locally