BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Kotlin Asynchronous Framework, Ktor 2.0, Released with New Plugins Feature

Kotlin Asynchronous Framework, Ktor 2.0, Released with New Plugins Feature

This item in japanese

After more than a year of development, Ktor 2.0, the Kotlin framework to create asynchronous client and server applications, has been released with new features and breaking changes.

On the server side, Ktor 2.0 introduces a new feature called Plugins, previously known as Features, to improve the extensibility of the framework. As the name suggests, it allows the implementation of the plugin architecture to extend functionalities in a simplified way.

It also introduces Kotlin/Native support to complement existing support for GraalVM added in Ktor 1.6. Other improvements include random port support, improved testing API, type-safe routing, XML Serialization and subroutes for plugins.

On the client side, Ktor 2.0 brings a simplified API to manage the common HTTP requests; the response is now of type HttpResponse and it is possible to access the response body simply with bodyAsText.

Retries are now built-in to the client that allows for specifying the amount of time between retries. Ktor 2.0 also adds a content negotiation feature on the client side, while on the server side it was already implemented.

In addition to the features above, the client also includes shortcut APIs for authentication such as basic() and bearer() helper functions.

In order to help with migration from previous versions to the new version 2.0, the Ktor team provides documentation and a migration tool included in IntelliJ IDEA which aims to fix all the issues related to the breaking changes and the refactored packages.

Creating a simple server with Ktor is as simple as:

fun main(args: Array<String>) {
    embeddedServer(Netty, 8080) {
        routing {
            get("/") {
                call.respondText("Hello, world!", ContentType.Text.Html)
            }
        }
    }.start(wait = true)
}

The code above runs an embedded web server on localhost:8080, installs a routing plugin and replies with "Hello, world" when receiving a GET HTTP request on the root path.

Ktor applications can be hosted in any servlet container with Servlet 3.0+ API support such as Tomcat, or standalone using Netty or Jetty. Ktor uses Kotlin coroutines to implement asynchronous programming with an intuitive imperative flow and all the server containers mentioned above are using asynchronous I/O facilities to avoid blocking threads.

Furthermore, Ktor supports the Websocket protocol, monitoring with Micrometer metrics, authorization with JWT and OAuth and a variety of template engines such as Mustache, Thymeleaf and Velocity.

Ktor is an official JetBrains product and is developed by the team at JetBrains along with contributions from the community.

Developers can learn more on how to create microservices-based applications with Ktor from this InfoQ tutorial by Hadi Hariri, VP of developer advocacy at JetBrains.

About the Author

Rate this Article

Adoption
Style

BT