IntelliJ IDEA provides the following capabilities for working with Ktor projects:
A wizard for creating new projects.
A dedicated run configuration for running and debugging Ktor projects.
Navigating to routes across a project.
Code editing assistance, including completion and renaming route names.
Assistance in automatic and manual testing of routes.
Create a new Ktor project
On the Welcome screen, click New Project.
Otherwise, from the main menu, select.
In the New Project wizard, choose Ktor from the list on the left.
On the right pane, you can specify the following settings:
Name: Specify a project name.
Location: Specify a directory for your project.
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: Leave this option enabled to add sample code for plugins added on the next page.
In this tutorial, we leave the default values for these settings. Click Next to go to the next page.
On the next page, you can choose a set of plugins - building blocks that provide common functionality of a Ktor application, for example, authentication, serialization and content encoding, compression, cookie support, and so on.
For now, let's install only the Routing plugin to handle incoming requests. Start typing routing at the top left search box, find Routing in the list, and click Add.
Click Finish and wait until IntelliJ IDEA generates a project and installs the dependencies.
Run a Ktor application
IntelliJ IDEA creates a Ktorrun/debug configuration automatically for newly created or imported Ktor projects. If necessary, you can disable creating a run configuration by using the Create Run Configuration automatically option on the page.
To run an application using the Ktor run configuration, do one of the following:
Select the Ktor run configuration in the main toolbar and click or press Shift+F10.
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
This means that the server is ready to accept requests at the http://0.0.0.0:8080 address.
Find routes with Search Everywhere
To find a specific route in your application using Search everywhere, follow the steps below:
Press Shift twice to open the search window.
Start typing a route name.
Navigate between a route and its usages
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 icon next to the path or URL and choose Go to declaration or usages from a popup.
IntelliJ IDEA will show related paths/URLs:
Show routes in the Endpoints tool window
To see all the routes defined in your Ktor application, you can use the Endpoints tool window:
Click the icon next to the path and choose Show all endpoints of module from a popup.
In the invoked Endpoints tool window, you can see all the routes defined in your application.
Complete route names
IntelliJ IDEA allows you to complete 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.
In IntelliJ IDEA, you can use rename refactoring to rename routes:
Place the caret at the route name and press Shift+F6.
Specify a new route name and click Refactor.
Create a test for a route
To create a test for a specific route, follow the steps below:
Click the icon next to the desired route and choose Generate test for current route from a popup.
In the invoked dialog, specify a test name and click OK. IntelliJ IDEA will create a test that should be implemented.
You can learn how to test a Ktor server from Testing.
Test a call in the HTTP editor
IntelliJ IDEA allows you to test Ktor routes using the HTTP client in code editor:
Click the icon next to the desired path and choose Generate request in HTTP Client.
In the opened file, adjust request parameters.
Click in the gutter to make a request.