Creating your first Micronaut GraalPy application

Learn how to create a Hello World Micronaut GraalPy application with a controller and a functional test.

Authors: Tomas Stupka

Micronaut Version: 4.6.3

1. Getting Started

In this guide, we will create a Micronaut application written in Java.

2. What you will need

To complete this guide, you will need the following:

3. Solution

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

4. Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide \
    --features=graalpy \
    --build=maven \
    --lang=java \
    --test=junit
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

If you use Micronaut Launch, select Micronaut Application as application type and add graalpy features.

If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application.

4.1. Python code

Part of the "Hello World" functionality will be provided by Python.

Create a Python file with the following code:

src/main/resources/org.graalvm.python.vfs/src/hello.py
def hello(txt): (1)
    return f"Hello {txt}"
1 The Python function we are going to call from Java

All files in the src/main/resources/org.graalvm.python.vfs/ directory will be accessible to the GraalPy runtime environment. The src subdirectory is automatically added to the Python search path, making it the ideal location for custom Python files in your application.

4.2. Java binding

Python code can be accessed programmatically using the GraalVM SDK Polyglot API, which enables you to embed Python into your applications.

In order to make it work, we first need a Java interface providing the intended binding to Python.

Create a Java interface with the following code:

src/main/java/example/micronaut/HelloModule.java
package example.micronaut;

import io.micronaut.graal.graalpy.annotations.GraalPyModule;

@GraalPyModule("hello") (1)
public interface HelloModule {
    String hello(String txt); (2)
}
1 The @GraalPyModule annotation indicates that the bean created from the interface is intended to import the hello.py Python module into GraalPy and and expose it to the Java code using the Target type mapping.
2 Java method matching the Python function hello(txt) in hello.py.

4.3. Controller

To create a microservice that responds with "Hello World" you also need a controller.

Create a controller with the following code:

src/main/java/example/micronaut/HelloController.java
package example.micronaut;

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") (1)
class HelloController {

    private final HelloModule hello;

    HelloController(HelloModule hello) {  (2)
        this.hello = hello;
    }

    @Get (3)
    @Produces(MediaType.TEXT_PLAIN) (4)
    String index() {
        return hello.hello("World"); (5)
    }
}
1 The class is defined as a controller with the @Controller annotation mapped to the path /hello.
2 Use constructor injection to inject a bean of type HelloModule.
3 The @Get annotation maps the index method to an HTTP GET request on /hello.
4 By default, a Micronaut response uses application/json as Content-Type. We are returning a String, not a JSON object, so we set it to text/plain with the @Produces annotation.
5 Use the HelloModule bean to call the Python function hello(txt) in hello.py.

4.4. Test

Create a test to verify that when you make a GET request to /hello you get Hello World as a response:

src/test/java/example/micronaut/HelloControllerTest.java
package example.micronaut;

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.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

@MicronautTest (1)
class HelloControllerTest {
    @Test
    void testHelloResponse(@Client("/") HttpClient client) { (2)
        String response = client.toBlocking().retrieve(HttpRequest.GET("/hello")); (3)
        assertEquals("Hello World", response);
    }

}
1 Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info.
2 Inject the HttpClient bean and point it to the embedded server.
3 Creating HTTP Requests is easy thanks to the Micronaut framework fluid API.

5. Testing the Application

To run the tests:

./mvnw test

6. Running the Application

To run the application, use the ./mvnw mn:run command, which starts the application on port 8080.

7. Next Steps

Read more about Micronaut Graalpy integration.

8. Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.

9. License

All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…​).