Featured image of post Build a First Micronaut Application

Build a First Micronaut Application

Learning Micronaut Journey - Part 2

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:

  1. Micronaut 4.2.0.
  2. JDK 17
  3. 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:

1
mn create-app Melon-BE --lang=kotlin --build=gradle

Exploring the Project Structure

Once the project is created, open it with Intellij IDEA. You’ll find a structure similar to the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
Melon-BE/
├── gradle/
├── src/
│   ├── main/
│   │   ├── kotlin/
│   │   │   └── melon/
│   │   │       └── Application.kt
│   │   └── resources/
│   │       ├── application.properties
│   │       └── logback.xml
│   └── test/
│       └── kotlin/
│           └── melon/
│               └── MelonBETest.kt
├── build.gradle
├── gradle.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
  • 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
package melon

import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces

@Controller("/hello")
class HelloController {

    @Get
    @Produces(MediaType.TEXT_PLAIN)
    fun index() = "Hello World"
}

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package melon

import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test

@MicronautTest
class HelloControllerTest(@Client("/") val client: HttpClient) {

    @Test
    fun testHello() {
        val request: HttpRequest<Any> = HttpRequest.GET("/hello")
        val body = client.toBlocking().retrieve(request)
        assertNotNull(body)
        assertEquals("Hello World", body)
    }
}

Testing the Application

To run the tests, execute the following command in the terminal:

1
./gradlew test

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:

1
./gradlew run

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:

1
curl http://localhost:8080/hello

You should receive a response like:

1
Hello World

Congratulations! You have successfully created and run your first Micronaut application using Kotlin.

Happy coding with Micronaut and Kotlin!

References

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy