Java / Gradle

Visualize an OpenAPI specification of your Micronaut application with Swagger UI

Learn how to generate an OpenAPI specification of your Micronaut application at build time and visualize it with Swagger UI.

Sergio del Amo
On this guide
In this section

Getting Started

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

What you will need

To complete this guide, you will need the following:

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.

Writing the Application

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

mn create-app example.micronaut.micronautguide \
    --features=openapi,swagger-ui,validation \
    --build=gradle \
    --lang=java \
    --test=junit
Note
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 openapi, swagger-ui, and validation features.

Note
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.

Micronaut OpenAPI

To use Micronaut OpenAPI, add the micronaut-openapi annotation processor and the Micronaut OpenAPI annotations as a compile-only dependency:

build.gradle
annotationProcessor("io.micronaut.openapi:micronaut-openapi")
implementation("io.micronaut.openapi:micronaut-openapi-annotations")

The micronaut-openapi-annotations dependency brings the Swagger annotations dependency transitively.

Latest Guide Controller

First, let’s create a controller that exposes the Micronaut Guides information.

Guides Model

We support different build tools per guide:

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

public enum BuildTool {

    GRADLE, MAVEN
}

A guide may be written in multiple languages:

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

public enum Language {

    GROOVY,
    JAVA,
    KOTLIN
}

Thus, we have several options per guide:

src/main/java/example/micronaut/Option.java

The following Java record represents a Micronaut Guide:

src/main/java/example/micronaut/Guide.java

Controller

Let’s create a controller for which we will generate an OpenAPI Specification at build time.

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

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

@Controller("/latest") // 
class LatestGuidesController {

    private static final List<Guide> GUIDES = Collections.singletonList(
            new Guide("Creating your first Micronaut application",
                    "Learn how to create a Hello World Micronaut application with a controller and a functional test.",
                    List.of("Iván López", "Sergio dle Amo"),
                    List.of("junit","getting_started","graalvm"),
                    List.of("Getting Started"),
                    LocalDate.of(2018, 5, 23),
                    "creating-your-first-micronaut-app",
                    "https://guides.micronaut.io/latest/creating-your-first-micronaut-app.html",
                    List.of(new Option(Language.JAVA, BuildTool.GRADLE, "https://guides.micronaut.io/latest/creating-your-first-micronaut-app-gradle-java.html"),
                            new Option(Language.GROOVY, BuildTool.GRADLE, "https://guides.micronaut.io/latest/creating-your-first-micronaut-app-gradle-groovy.html"),
                            new Option(Language.KOTLIN, BuildTool.GRADLE, "https://guides.micronaut.io/latest/creating-your-first-micronaut-app-gradle-kotlin.html"),
                            new Option(Language.JAVA, BuildTool.MAVEN, "https://guides.micronaut.io/latest/creating-your-first-micronaut-app-maven-java.html"),
                            new Option(Language.GROOVY, BuildTool.MAVEN, "https://guides.micronaut.io/latest/creating-your-first-micronaut-app-maven-groovy.html"),
                            new Option(Language.KOTLIN, BuildTool.MAVEN, "https://guides.micronaut.io/latest/creating-your-first-micronaut-app-maven-kotlin.html")
                            ))
    );

    @Get("/guides.json") // 
    List<Guide> latestGuides() {
        return GUIDES;
    }
}

We could test such a controller with:

src/test/java/example/micronaut/LatestGuidesControllerTest.java

@OpenApiSpecification

The Micronaut OpenAPI integration requires a class to be annotated with @OpenApiSpecification. Modify Application.java and annotate it.

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

import io.micronaut.runtime.Micronaut;
import io.swagger.v3.oas.annotations.*;
import io.swagger.v3.oas.annotations.info.*;
import io.swagger.v3.oas.annotations.servers.Server;

@OpenAPIDefinition(
    info = @Info(
            title = "micronaut-guides",
            version = "1.0"
    ), servers = @Server(url = "https://guides.micronaut.io")
) // 
public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class, args);
    }
}

OpenAPI Generated at Compile Time

By adding the @OpenApiSpecification annotation to Application, an OpenAPI specification is generated at compile time. You can test it as follows:

src/test/java/example/micronaut/OpenApiGeneratedTest.java

Expose the OpenAPI Specification

We can expose the OpenAPI specification generated at compile time as a static resource with the following configuration:

src/main/resources/application.properties
micronaut.router.static-resources.swagger.paths=classpath:META-INF/swagger
micronaut.router.static-resources.swagger.mapping=/swagger/**

You can test that the OpenAPI specification is available:

src/test/java/example/micronaut/OpenApiExposedTest.java

Swagger UI

To simplify the visualization of the OpenAPI specification, we are going to use Swagger UI.

Swagger UI allows anyone — be it your development team or your end consumers — to visualize and interact with the API’s resources without having any of the implementation logic in place. It’s automatically generated from your OpenAPI (formerly known as Swagger) Specification, with the visual documentation making it easy for back end implementation and client-side consumption.

To generate Swagger UI HTML, create an openapi.properties file in the root of your project and add:

swagger-ui.enabled=true

You can write a test to verify that Swagger UI HTML is generated at compile time.

java/src/test/java/example/micronaut/SwaggerUiGeneratedTest.java

Expose Swagger UI

We can expose the Swagger UI HTML generated at compile time as a static resource with the following configuration:

src/main/resources/application.properties
micronaut.router.static-resources.swagger-ui.paths=classpath:META-INF/swagger/views/swagger-ui
micronaut.router.static-resources.swagger-ui.mapping=/swagger-ui/**

You can test that Swagger UI is available:

java/src/test/java/example/micronaut/SwaggerUiTest.java

Home Controller

Next, create a controller that redirects to the Swagger UI endpoint.

java/src/main/java/example/micronaut/HomeController.java

Write a test that verifies the redirection and checks that the endpoint is not included in the OpenAPI specification:

java/src/test/java/example/micronaut/HomeControllerTest.java

Testing the Application

To run the tests:

./gradlew test

Then open build/reports/tests/test/index.html in a browser to see the results.

Running the Application

To run the application, use the ./gradlew run command, which starts the application on port 8080.

If you go to http://localhost:8080, you will see the Swagger UI visualization of the compile-time generated OpenAPI specification.

Generate a Micronaut Application Native Executable with GraalVM

We will use GraalVM, an advanced JDK with ahead-of-time Native Image compilation, to generate a native executable of this Micronaut application.

Compiling Micronaut applications ahead of time with GraalVM significantly improves startup time and reduces the memory footprint of JVM-based applications.

Note
Only Java and Kotlin projects support using GraalVM’s native-image tool. Groovy relies heavily on reflection, which is only partially supported by GraalVM.

GraalVM Installation

The easiest way to install GraalVM on Linux or Mac is to use SDKMan.io.

Java 25
sdk install java 25.0.2-graal

For installation on Windows, or for a manual installation on Linux or Mac, see the GraalVM Getting Started documentation.

The previous command installs Oracle GraalVM, which is free to use in production and free to redistribute, at no cost, under the GraalVM Free Terms and Conditions.

Alternatively, you can use the GraalVM Community Edition:

Java 25
sdk install java 25.0.2-graalce

Native Executable Generation

To generate a native executable using Gradle, run:

./gradlew nativeCompile

The native executable is created in build/native/nativeCompile directory and can be run with build/native/nativeCompile/micronautguide.

It is possible to customize the name of the native executable or pass additional parameters to GraalVM:

build.gradle

Next Steps

Explore more features with Micronaut Guides.

Help with the Micronaut Framework

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

License

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