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.
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:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 21 or greater installed with
JAVA_HOMEconfigured appropriately
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.
-
Download and unzip the source
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:
<!-- 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:
@ConfigurationProperties
|
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:
greeting.template=Hola, %s!POJO
Create a POJO that uses Jackson Annotations.
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.
Service Test
Create a test to verify validation works as expected.
@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.
Tests
Declarative HTTP Client
You can use Spring annotations with a declarative Micronaut HTTP Client.
and use it in a test:
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:
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.
<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:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>Controller
Controller Test
Add a test that proves you can use org.springframework.ui.Model and render server-side HTML.
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). |