IntelliJ IDEA 2021.2 Help

Micronaut

Micronaut is a modern Java framework for writing microservice and serverless applications. IntelliJ IDEA provides the following:

  • Coding assistance specific to the Micronaut API and the configuration file parameters. For example, when writing query methods, generating HTTP requests for defined endpoints, and so on.

  • Integration with the Bean Validation and Endpoints tool windows.

  • A dedicated project creation wizard based on launch.micronaut.io.

  • A dedicated run configuration for Micronaut applications.

Create a new Micronaut project

  1. From the main menu, select File | New | Project.

  2. In the New Project wizard, select Create <b>Micronaut</b> applications and choose the default Server URL: https://launch.micronaut.io.

    New Quarkus project wizard

    Click Next.

  3. Configure the necessary Micronaut project settings. You can choose from several application types. If you want a simple demo project, select the basic Micronaut application and click Next.

  4. Select the necessary features for your application and click Next.

  5. If necessary, change the project name, location, and other settings. Click Finish.

The generated project contains only the main Application class and everything necessary to run the application. IntelliJ IDEA recognizes it and adds the Run icon to the gutter, which you can click to run the application.

The default Micronaut application main class

However, an empty project is boring. Let's make the application return Hello World! in response to an HTTP GET request.

Add a Micronaut HTTP controller

  1. Right-click the directory with the main class (be default, it is src/main/java/com/example) and select New | Java Class.

  2. Type the name of the class HelloController and press Enter.

  3. Copy the following code into the created file:

    package com.example; 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") public class HelloController { @Get(produces = MediaType.TEXT_PLAIN) public String sayHello() { return "Hello World!"; } }
    • The @Controller annotation defines the class as an HTTP controller mapped to the /hello endpoint.

    • The @Get annotation maps all HTTP GET requests for this endpoint to the sayHello() method and sets the Content-Type of the response to text/plain.

    • The sayHello() method returns the string Hello World!.

IntelliJ IDEA recognizes the HTTP controller and marks it with The HTTP Controller icon in the gutter. You can also click The Open in HTTP Client icon to generate an HTTP request for the endpoint and open it in a separate HTTP client editor tab. IntelliJ IDEA provides other gutter icons, for example:

  • The Navigate to event listeners icon Navigate to event listeners

  • The Navigate to event publisher icon Navigate to event publisher

  • The Navigate to the autowired dependencies icon Navigate to the autowired dependencies

To see all the endpoints defined in your application, open the Endpoints tool window. For example, here is the /hello endpoint:

The Endpoints tool window with the sample /hello endpoint

Run the Micronaut application

IntelliJ IDEA creates a Micronaut run configuration that executes the necessary Maven goal or Gradle task.

  • Select the Micronaut run configuration in the main toolbar and click the Run button or press Shift+F10.

    Alternatively, you can press Alt+Shift+F10 and select the necessary run configuration.

    If successful, you should see the output log in the Run tool window.

    Micronaut application running in the Run tool window

By default, the application starts on http://localhost:8080. Open this address in a web browser and you will see the standard error response because the application root doesn't handle GET requests:

{"message":"Page Not Found","_links":{"self":{"href":"/","templated":false}}}

However, the application has an HTTP controller that responds to GET requests. If you open the http://localhost:8080/hello endpoint, the application will respond with Hello World!.

Last modified: 02 August 2022