Created
October 18, 2018 22:33
-
-
Save felix19350/1acb1c69102f5880ce0440f058f2352f to your computer and use it in GitHub Desktop.
Ktor API versioning via content type headers examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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