Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

What Micronaut is

Micronaut is a framework for server-side Java applications: REST APIs, microservices, command-line tools. It accepts HTTP requests, turns objects into JSON and back, works with a database, checks access rights and calls other services. Its key feature is that it does almost all of its preparation work at compile time, not at startup, so the application starts in about a second and uses little memory.

Work at compile time

A framework has to know which classes are controllers, which objects to pass to whom, and which fields a record has for JSON. Spring finds this out at startup: it scans classes, reads annotations through reflection (inspecting a class while the program runs) and builds proxies. Micronaut does the same in advance, through annotation processors. These are plugins for the javac compiler: while your code compiles, they read annotations and generate extra classes.

For example, for an ArticleController, these files appear in target/classes/com/example/ after the build:

$ArticleController$Definition.class
$ArticleController$Definition$Exec.class
ArticleController.class

The $ArticleController$Definition class is a ready-made "recipe" for creating the controller: which constructor to call and what to pass in. At startup Micronaut simply reads these recipes.

What the approach givesWhy
Fast startupNo need to scan classes and read annotations
Low memoryNo reflection caches and dynamic proxies
Earlier errorsSome mistakes show up at compile time
Native buildsWithout reflection, GraalVM easily compiles the app into an executable

How it differs from Spring Boot

The annotations are similar, so moving over from Spring is easy:

TaskSpring BootMicronaut
Controller@RestController@Controller
GET route@GetMapping@Get
Component@Service, @Component@Singleton
Settings@ConfigurationProperties@ConfigurationProperties
RepositorySpring DataMicronaut Data
When DI is wiredat startupat compile time

The price is that after changing dependencies in pom.xml you sometimes need a full rebuild, and the annotation processors must be registered in the build. Several lessons have a "Common mistake" about this.

Version used in the course

The course targets Micronaut 5.2 and Java 25: Micronaut 5 requires at least JDK 25. The build tool is Maven, tests use JUnit. Check your Java:

java -version

The command should show version 25 or newer. You do not need to install Maven separately: the project includes the mvnw wrapper, which downloads Maven by itself.

What we will build

Throughout the course we build a REST API for a blog:

  1. Articles at /articles: list, search, create, update, delete.
  2. Comments on articles and an H2 database through Micronaut Data JDBC.
  3. Input validation, error handling, settings.
  4. An HTTP client, tests, password and JWT login, scheduled jobs, health checks.
  5. Building a jar, plus an explanation of GraalVM native images.

We will send requests to the API with curl, which is available on Windows 10+, macOS and Linux.

Common mistake

If you run the build on an old Java, Maven fails before it even compiles your code. This is what JDK 16 prints:

java.lang.UnsupportedClassVersionError: io/micronaut/maven/testresources/TestResourcesLifecycleExtension
has been compiled by a more recent version of the Java Runtime (class file version 69.0),
this version of the Java Runtime only recognizes class file versions up to 60.0

Class file version 69 means Java 25. Install JDK 25 and make sure the JAVA_HOME variable points to it.

Thanks for reading!

Donate to the site — we work hard for you.