Adding a route

To begin adding endpoints to our service, we will install a routing feature into our application.

  1. We can do this using an available Application.routing() function. The following code shows this:
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
HttpClient(Jetty) {
routing {
// add routes here

}
}
}

Because Application.routing() takes in a configuration function, we can call routing {} with a lambda, which will enable us to configure the available endpoints for our service.

  1. Now, we'll add a route that will return "Hello World" when the base endpoint of our service is accessed. We'll do this by adding a get() call within our routing {} block. By passing "/" to get(), we define what happens at the root URL path:
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
HttpClient(Jetty) {
routing {
get("/") {
call.respondText("Hello World",
ContentType.Text.Plain)

}
}
}
}

When the "/" path is accessed, our route will respond with "Hello World". We will see this in action by deploying our service locally, which we'll take a look at in the next section.