Creating your first Micronaut Groovy app

Learn how to create a Hello World Micronaut app with Groovy with a controller and a functional test.

Authors: Sergio del Amo

Micronaut Version: 2.0.0.RC1

1 Getting Started

This guide uses Micronaut 2.x. You can read this tutorial for Micronaut 1.x.

In this guide we are going to create a Micronaut app written in Groovy.

1.1 What you will need

To complete this guide, you will need the following:

  • Some time on your hands

  • A decent text editor or IDE

  • JDK 1.8 or greater installed with JAVA_HOME configured appropriately

1.2 Solution

We recommend you to follow the instructions in the next sections and create the app step by step. However, you can go right to the completed example.


Then, cd into the complete folder which you will find in the root project of the downloaded/cloned project.

2 Writing the App

Create a Groovy Micronaut app using the Micronaut Command Line Interface.

mn create-app example.micronaut.complete --lang=groovy

The previous command creates a micronaut app with the default package example.micronaut in a folder named complete.

Due to the --lang=groovy flag, it generates a Groovy Micronaut app that uses the Gradle build system. However, you could use other build tools such as Maven or other programming languages such as Java or Kotlin.

3 Controller

In order to create a microservice that responds to "Hello World" you first need a controller.

Create a Controller:

package example.micronaut

import groovy.transform.CompileStatic
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import io.micronaut.http.MediaType

@Controller("/hello") (1)
class HelloController {
    @Get("/") (2)
    @Produces(MediaType.TEXT_PLAIN) (3)
    String index() {
        "Hello World" (4)
1 The class is defined as a controller with the @Controller annotation mapped to the path /hello
2 The @Get annotation is used to map the index method to all requests that use an HTTP GET
3 By default a Micronaut’s response uses application/json as Content-Type. We are returning a String not a JSON object. Because of that, we set it to text/plain.
4 A String "Hello World" is returned as the result

4 Test

Create a Spock feature method which verifies that when you do a GET request to /hello you get Hello World as a response:

package example.micronaut

import io.micronaut.http.HttpRequest
import io.micronaut.http.client.RxHttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.annotation.MicronautTest
import spock.lang.Specification

import javax.inject.Inject

@MicronautTest (1)
class HelloControllerSpec extends Specification {

    RxHttpClient client (2)

    void "test hello world response"() {
        HttpRequest request = HttpRequest.GET('/hello') (3)
        String rsp = client.toBlocking().retrieve(request)

        rsp == "Hello World"
1 Annotate the class with @MicronautTest so Micronaut will initialize the application context and the embedded server.
2 Inject the RxHttpClient bean. It is used the execute an HTTP call to the controller.
3 Creating HTTP Requests is easy thanks to Micronaut’s fluid API.

5 Testing the Application

To run the tests:

$ ./gradlew test
$ open build/reports/tests/test/index.html

6 Running the Application

To run the application use the ./gradlew run command which will start the application on port 8080.

7 Next steps

Read more about Micronaut testing.

8 Help with Micronaut

Object Computing, Inc. (OCI) sponsored the creation of this Guide. A variety of consulting and support services are available.

OCI is Home to Micronaut.

Meet the Team