Skip to content

Instantly share code, notes, and snippets.

@felix19350
Created October 18, 2018 22:33
Show Gist options
  • Save felix19350/1acb1c69102f5880ce0440f058f2352f to your computer and use it in GitHub Desktop.
Save felix19350/1acb1c69102f5880ce0440f058f2352f to your computer and use it in GitHub Desktop.
Ktor API versioning via content type headers examples
import io.ktor.http.ContentType
// Resource definition
data class MyResource(val name:String)
// Content-type definition
object MyContentTypes {
object v1 {
val MyResource = ContentType("application", "vnd.app.myResource-v1+json")
}
}
// Register a route that only accepts this particular resource
fun Route.myRoute{
accept(MyContentTypes.v1.MyResource) {
get("/my-resource") {
call.respond(MyResource("42"))
}
}
}
// Sample application module - The important bit is registering our custom content-type
fun Application.module(){
install(ContentNegotiation) {
register(WebgsContentTypes.v1.SatelliteDefinition, JacksonConverter())
}
install(Routing) { myRoute() }
}
// Main
fun main(args: Array<String>) {
embeddedServer(Netty, 8080, module = Application::module).start(wait = true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment