In this blog post, I’ll be following the steps outlined in the CREATING YOUR FIRST MICRONAUT APPLICATION to creat my first Micronaut application using Kotlin by myself.
Prerequisites
Before we get started, make sure the following prerequisites are installed on your system:
- Micronaut 4.2.0.
- JDK 17
- IntelliJ IDEA IDE for Kotlin development.
Setting Up My Micronaut Project
Micronaut provides a convenient command-line tool for creating a new project. Open your terminal and run the following command to create a new project named Melon-BE
:
|
|
Exploring the Project Structure
Once the project is created, open it with Intellij IDEA. You’ll find a structure similar to the following:
|
|
src/main/kotlin/melon/Application.kt
: The main class that bootstraps your Micronaut application.src/main/resources/application.properties
: Configuration file for your Micronaut application.src/test/kotlin/melon/MelonBETest.kt
: A test class for the Melon.
Creating a New Controller
To create a microservice that responds with Hello World
, you need a controller. Create a Controller at src/main/kotlin/melon/HelloController.kt
:
|
|
Create a test at src/test/kotlin/melon/HelloControllerTest.kt
to verify that when you make a GET request to /hello
, you get Hello World
as a response:
|
|
Testing the Application
To run the tests, execute the following command in the terminal:
|
|
Then open build/reports/tests/test/index.html
in a browser to see the results.
Running Your Micronaut Application
To run your Micronaut application, navigate to the project directory and execute the following command:
|
|
This command builds and runs your Micronaut application. Once the application is started, you should see output indicating that the server is running.
Visit http://localhost:8080/hello in your web browser or use a tool like cURL to send an HTTP GET request:
|
|
You should receive a response like:
|
|
Congratulations! You have successfully created and run your first Micronaut application using Kotlin.
Happy coding with Micronaut and Kotlin!