IntelliJ IDEA 2022.3 Help

Ktor

Ktor is a web application framework for creating connected systems. You can use it to create server-side as well as client-side applications. It supports multiple platforms, including JVM, JavaScript, and Kotlin/Native.

IntelliJ IDEA provides the following capabilities for working with Ktor projects:

Create a new Ktor project

  1. On the Welcome screen, click New Project.

    Otherwise, from the main menu, select File | New | Project.

  2. In the New Project wizard, choose Ktor from the list on the left.

  3. On the right pane, you can specify the following settings:

    Ktor Project Settings
    • Name: Specify a project name.

    • Location: Specify a directory for your project.

    • Build System: Choose the desired build system. This can be Gradle with Kotlin or Groovy DSL, or Maven.

    • Website: Specify a domain used to generate a package name.

    • Artifact: This field shows a generated artifact name.

    • Ktor version: Choose the required Ktor version.

    • Engine: Select an engine used to run a server.

    • Configuration in: Choose whether to specify server parameters in code or in a HOCON file.

    • Add sample code: Use this option to add sample code for plugins, which will be added on the next page.

  4. On the next page, you can choose a set of plugins - building blocks that provide common functionality of a Ktor application, such as authentication, serialization and content encoding, compression, cookie support, and so on.

    Ktor plugins

    Click Create and wait until IntelliJ IDEA generates a project and installs the dependencies.

Run a Ktor application

IntelliJ IDEA creates a Ktor run/debug configuration automatically for newly created or imported Ktor projects. If necessary, you can manage creating run configurations by using the Create Run Configuration automatically option on the Settings/Preferences | Languages & Frameworks | Ktor page.

To run an application using the Ktor run configuration, follow these steps:

  1. Select the Ktor run configuration in the main toolbar and click the Run button or press Shift+F10.

  2. Wait until IntelliJ IDEA runs the application. The Run tool window should show the following message:

    [main] INFO ktor.application - Responding at http://0.0.0.0:8080
    Run tool window

    This means that the server is ready to accept requests at the http://0.0.0.0:8080 address.

To find a specific route in your application using Search everywhere, follow the steps below:

  1. Press Shift twice to open the search window.

  2. Start typing a route name.

    Search Everywhere

    To narrow down your search, click the Filter icon Filter on the window toolbar and select URLs.

To navigate between a route and its usages in a project, do one of the following:

  • Place the caret at the desired path or URL and press Ctrl+B.

  • Keeping Ctrl pressed, hover the mouse pointer over the path or URL. When a URL turns into a hyperlink, click it without releasing the key.

  • Click the Globe icon next to the path or URL and choose Go to declaration or usages from a popup.

IntelliJ IDEA will show related paths/URLs:

Find usages

To see all HTTP or WebSocket endpoints defined in your Ktor application, use the Endpoints tool window:

  1. Click the Globe icon next to the path and choose Show all endpoints of module from a popup.

    Show endpoints
  2. In the invoked Endpoints tool window, you can see all the routes defined in your application.

    Ktor endpoints

    If a Ktor server is running, you can make a request using the HTTP Client to see a response immediately or open it in the editor.

    Ktor endpoints HTTP client

    On the OpenAPI tab, you can see an OpenAPI specification for this route.

Generate code

Generate code for plugins

IntelliJ IDEA allows you to generate code for Ktor plugins using the code completion popup.

  1. Place the caret inside a module, start typing install and then choose the desired plugin from the list.

    Install a plugin
  2. Press Enter. IntelliJ IDEA generates code for the selected plugin and adds the required dependencies to the build script.

    Gode generated for a plugin

Create tests

IntelliJ IDEA allows you to generate tests for modules, groups of routes, or individual routes. This functionality is supported for both HTTP and WebSocket endpoints. To generate tests, follow the steps below:

  1. Place the caret at the desired module or route, press Alt+Enter, and choose one of the following actions:

    • Create test for Ktor module

    • Create test for Ktor routes

    • Create test for Ktor route

    Generate test for route
    Generate test for route
    Generate test for route
  2. In the invoked dialog, you can specify a test class name, choose the desired routes, and edit individual test names.

    Generate tests

    Click OK.

  3. IntelliJ IDEA will create a test class containing tests requests for the selected routes.

    Generate tests

    You can learn how to test a Ktor server from Testing.

Edit code

Complete route names

IntelliJ IDEA allows you to complete the names of routes:

  • Place the caret at the parameter that accepts a URL and press Ctrl+Space. The editor will show you the available routes.

    Complete route name

Rename routes

In IntelliJ IDEA, you can use rename refactoring to rename routes:

  1. Place the caret at the route name and press Shift+F6.

  2. Specify a new route name and click Refactor.

    Rename route

Extract an application module

To extract code placed inside embeddedServer to a separate application module, follow the steps below:

  1. Place the caret at the embeddedServer function call and press Alt+Enter.

  2. Choose Extract Application module from the current embeddedServer block and press Enter.

  3. In the invoked dialog, specify a module name and click Extract. IntelliJ IDEA creates an extension function on the Application class and passes a reference to this function as the module parameter, for example:

    fun main() { embeddedServer(Netty, port = 8080) { routing { get("/") { call.respondText("Hello, world!") } } }.start(wait = true) }
    fun main() { embeddedServer(Netty, port = 8080, module = Application::module).start(wait = true) } fun Application.module() { routing { get("/") { call.respondText("Hello, world!") } } }

OpenAPI

An OpenAPI Specification (OAS) is a description format for REST APIs. The Ktor plugin provides the ability to generate OpenAPI documentation for server-side Ktor applications. The plugin generates paths for all application endpoints defined using both string path patterns or type-safe routes. For generating better OpenAPI docs, the plugin analyzes the code of endpoints in a server application, for example:

Generate an OpenAPI specification

To generate an OpenAPI specification for a Ktor application, follow the steps below:

  1. Place the caret at the embeddedServer function call and press Alt+Enter.

  2. Select Generate OpenAPI documentation for current module from the popup and press Enter.

    Generate OpenAPI documentation

    IntelliJ IDEA will generate an OpenAPI specification in application resources by the following path: resources/openapi/documentation.yaml.

    OpenAPI documentation

Preview OpenAPI and test your application

You can preview an OpenAPI specification using the integrated Swagger UI as described in Preview an OpenAPI specification. The generated Swagger UI can be used to test your application's endpoints as follows:

  1. Run the application.

  2. Expand the desired endpoint in the preview and make a request.

Test endpoints in the HTTP editor

IntelliJ IDEA allows you to test HTTP and WebSocket endpoints using the HTTP client in code editor:

  1. Place the caret at the desired path, press Alt+Enter, and choose Generate request in HTTP Client.

    Generate request
  2. In the opened file, adjust request parameters.

    Adjust request parameters
  3. Click The Run button in the gutter to make a request.

    Run request

Migrate projects

Migrate a project

In IntelliJ IDEA, you can migrate your Ktor project to the latest version in one of the following ways:

  • When opening a project that uses a previous Ktor version, IntelliJ IDEA suggests you migrate this project or a specific module to the latest version.

    Migrate project popup

    You can migrate a project/module or click Actions to configure a migration level or cancel migration and disable this popup.

  • Press Ctrl+Shift+A, find the Migrate Ktor to Latest Version action, and press Enter.

Configure a migration level

  • To configure a migration level or disable migrations at all, use a corresponding option on the Settings/Preferences | Languages & Frameworks | Ktor page.

Last modified: 05 December 2022