Migrate to Micronaut 3 from Micronaut 2
In this guide we'll look at using OpenRewrite to perform an automated migration from Micronaut 2.x to Micronaut 3.x

Example Configuration

Gradle
Maven
build.gradle
1
plugins {
2
id("org.openrewrite.rewrite") version("5.12.0")
3
}
4
5
rewrite {
6
activeRecipe("org.openrewrite.java.micronaut.Micronaut2to3Migration")
7
}
8
9
repositories {
10
mavenCentral()
11
}
12
13
dependencies {
14
rewrite("org.openrewrite.recipe:rewrite-micronaut:1.3.0")
15
}
Copied!
pom.xml
1
<project>
2
<build>
3
<plugins>
4
<plugin>
5
<groupId>org.openrewrite.maven</groupId>
6
<artifactId>rewrite-maven-plugin</artifactId>
7
<version>4.13.0</version>
8
<configuration>
9
<activeRecipes>
10
<recipe>org.openrewrite.java.micronaut.Micronaut2to3Migration</recipe>
11
</activeRecipes>
12
</configuration>
13
<dependencies>
14
<dependency>
15
<groupId>org.openrewrite.recipe</groupId>
16
<artifactId>rewrite-micronaut</artifactId>
17
<version>1.3.0</version>
18
</dependency>
19
</dependencies>
20
</plugin>
21
</plugins>
22
</build>
23
</project>
Copied!
At this point, you're ready to execute the migration by running mvn rewrite:run or gradlew rewriteRun. After running the migration you can inspect the results with git diff (or equivalent), manually fix anything that wasn't able to be migrated automatically, and commit the results.

Before and After

For the full list of changes this recipe will make, see its reference page.
Deprecated Exception Handler constructors
Several micronaut ExceptionHandlers have been refactored such that the no-args constructor has been deprecated and replaced with one taking an ErrorResponseProcessor.
Before
After
1
import io.micronaut.validation.exceptions.ConstraintExceptionHandler;
2
public class ApiClientValidationExceptionHandler extends ConstraintExceptionHandler {
3
...
4
}
Copied!
1
import io.micronaut.validation.exceptions.ConstraintExceptionHandler;
2
import jakarta.inject.Inject;
3
4
public class ApiClientValidationExceptionHandler extends ConstraintExceptionHandler {
5
6
@Inject
7
public ApiClientValidationExceptionHandler(ErrorResponseProcessor errorResponseProcessor) {
8
super(errorResponseProcessor);
9
}
10
...
11
}
Copied!
Subclasses Returned From Factories Not Injectable
It is no longer possible to inject the internal implementation type from beans produced via factories. The type returned from the factory or any of its supertypes are able to be injected.
Before
After
1
import java.util.concurrent.ForkJoinPool;
2
import java.util.concurrent.ExecutorService;
3
import javax.inject.Singleton;
4
5
public class ExecutorFactory {
6
7
@Singleton
8
public ExecutorService executorService() {
9
return ForkJoinPool.commonPool();
10
}
11
}
Copied!
1
import java.util.concurrent.ForkJoinPool;
2
import java.util.concurrent.ExecutorService;
3
import javax.inject.Singleton;
4
5
public class ExecutorFactory {
6
7
@Singleton
8
public ForkJoinPool executorService() {
9
return ForkJoinPool.commonPool();
10
}
11
}
Copied!
Reflective Bean Map
In several places in Micronaut, it is required to get a map representation of your object. In previous versions, a reflection based strategy was used to retrieve that information if the class was not annotated with @Introspected. That functionality has been removed and it is now required to annotate classes with @Introspected that are being used in this way. Any class may be affected if it is passed as an argument or returned from any controller or client, among other use cases.
Controller
Dto Before
Dto After
1
import io.micronaut.http.annotation.Controller;
2
import io.micronaut.http.annotation.Post;
3
4
@Controller("/orders")
5
public class OrdersController {
6
@Get("/order")
7
public Order getOrder(Long orderId) {
8
...
9
return order;
10
}
11
}
Copied!
1
public class Order {
2
...
3
}
Copied!
1
import io.micronaut.core.annotation.Introspected;
2
3
@Introspected
4
public class Order {
5
...
6
}
Copied!

Known Limitations

Unsupported Functionality
Issue
Maybe add RxJava2 dependencies
#26
Non-Inherited annotations from super-class method are not copied to overriden method
#13
DefaultContext doCreateBean deprecated
#23
DefaultHttpHostResolver constructor arg Provider to BeanProvider
#22
Change io.micronaut.core.beans.PropertyDescriptor to BeanProperty
#21
Last modified 10d ago