Java / Maven

Run a Spring Boot application as a Micronaut application

Write an application using Spring Boot annotations and compute it into a Micronaut application at compilation time.

Sergio del Amo
On this guide
In this section

Getting Started

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

You will write an application that includes no dependencies on Micronaut itself in the source code (only Spring dependencies) but is computed into a Micronaut application at compilation time.

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=spring-web,spring-boot,views-thymeleaf,validation \
    --build=maven \
    --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 spring-web, spring-boot, views-thymeleaf, 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.

Dependencies

Micronaut for Spring allows you to use traditional Spring annotations, which are mapped to Micronaut annotations at compilation time. This allows you to write an application that can be imported to another Spring or Micronaut application without change.

To use Spring Framework Annotations, write Spring MVC controllers, and support Spring Boot features, add both features spring-web and spring-boot when you create the application with Micronaut CLI and/or Micronaut Launch. These features include the following dependencies:

pom.xml
<!-- Add the following to your annotationProcessorPaths element -->
<path>
    <groupId>io.micronaut.spring</groupId>
    <artifactId>micronaut-spring-annotation</artifactId>
</path>
<!-- Add the following to your annotationProcessorPaths element -->
<path>
    <groupId>io.micronaut.spring</groupId>
    <artifactId>micronaut-spring-web-annotation</artifactId>
</path>
<!-- Add the following to your annotationProcessorPaths element -->
<path>
    <groupId>io.micronaut.spring</groupId>
    <artifactId>micronaut-spring-boot-annotation</artifactId>
</path>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>io.micronaut.spring</groupId>
    <artifactId>micronaut-spring-web</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.micronaut.spring</groupId>
    <artifactId>micronaut-spring-boot</artifactId>
    <scope>runtime</scope>
</dependency>

Application

Create an Application class:

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

@ConfigurationProperties

java/src/main/java/example/micronaut/GreetingConfiguration.java
Tip
The @Prototype annotation is a synonym for @Bean because the default scope in a Micronaut application is prototype.

Configuration

Add some configuration which will populate this GreetingConfiguration:

src/main/resources/application.properties
greeting.template=Hola, %s!

POJO

Create a POJO that uses Jackson Annotations.

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

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.micronaut.serde.annotation.Serdeable;

@Serdeable
public class Greeting {

    private final long id;
    private final String content;

    @JsonCreator
    public Greeting(@JsonProperty("id") long id, @JsonProperty("content") String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

Service

Add a service that showcases configuration injection and validation.

java/src/main/java/example/micronaut/GreetingService.java

Service Test

Create a test to verify validation works as expected.

java/src/test/java/example/micronaut/GreetingServiceTest.java

@RestController

Micronaut Spring only supports @RestController semantics. A @RestController request handling method serializes return objects into an HTTP response. Create a controller that uses the following Spring annotations.

java/src/main/java/example/micronaut/GreetingController.java

Tests

Declarative HTTP Client

You can use Spring annotations with a declarative Micronaut HTTP Client.

java/src/test/java/example/micronaut/GreetingClient.java

and use it in a test:

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

import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;

import jakarta.inject.Inject;
import static org.junit.jupiter.api.Assertions.*;

@MicronautTest
class GreetingControllerTest {

    @Inject
    GreetingClient greetingClient;

    @Test
    void testGreetingService() {
        assertEquals(
                "Hola, John!",
                greetingClient.greet("John").getContent()
        );
    }
}

@Scheduled

You can use the @Scheduled annotation to schedule a recurring task. Create a Job:

java/src/main/java/example/micronaut/GreetingJob.java

Server side HTML Rendering

Thymeleaf view

Views

To use the Thymeleaf Java template engine to render views in a Micronaut application, add the following dependency on your classpath.

pom.xml
<dependency>
    <groupId>io.micronaut.views</groupId>
    <artifactId>micronaut-views-thymeleaf</artifactId>
    <scope>compile</scope>
</dependency>

Create a Thymeleaf view to render the UI for the controller:

src/main/resources/views/home.html
<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
</head>
<body>
    <h1 th:text="${message}"></h1>
</body>
</html>

Controller

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

Controller Test

Add a test that proves you can use org.springframework.ui.Model and render server-side HTML.

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

Running the Application

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

Execute the API:

 curl "http://localhost:8080/greeting?name=Sergio"
{"id":1,"content":"Hola, Sergio!"}

If you wait 30 seconds, you will see a log statement from GreetingJob.

Next Steps

Read more about Micronaut Spring.

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