Contents
Micronaut
A lightweight framework for microservices: compile-time dependency injection, HTTP, data and testing.
1.Getting started
- What Micronaut isWhy Micronaut exists, how compile-time dependency injection works, how it differs from Spring Boot and what we will build in this course.
- Creating and running a projectHow to create a Micronaut project at launch.micronaut.io or with mn create-app, what it contains and how to run it.
- Controllers and routesHow to write a Micronaut controller with @Controller and @Get, and how routes and HTTP methods are declared.
- Request parametersHow a Micronaut controller receives path variables, query string parameters and headers: @PathVariable, @QueryValue, @Header and default values.
- JSON, records and @SerdeableHow a Micronaut controller accepts and returns JSON: Java records, the @Serdeable annotation from Micronaut Serialization, @Body and a 201 Created response.
2.Application basics
- Dependency injection and beansWhat a bean is in Micronaut, how @Singleton and constructor injection work, which scopes exist and how to make a bean from a third-party class with @Factory.
- Configuration and environmentsThe application.properties file in Micronaut, reading values with @Value and @ConfigurationProperties, OS environment variables and the dev and prod environments.
- ValidationValidating input in Micronaut: adding micronaut-validation, @NotBlank and @Size on a record, @Valid in a controller and the 400 response.
- Error handlingHow to return 404 and other codes in Micronaut: Optional, HttpStatusException, your own exception and an ExceptionHandler.
3.Database
- Micronaut Data JDBC and H2Adding the H2 database and Micronaut Data JDBC: dependencies, data source settings, an @MappedEntity entity and a CrudRepository.
- Repository queriesMicronaut Data queries derived from method names (findByTitleContains, countBy, OrderBy), custom SQL with @Query, Pageable pagination and article comments.
- TransactionsThe @Transactional annotation with Micronaut Data: several queries as one unit, rollback on an exception and the limits of AOP.
4.Clients, tests and security
- Declarative HTTP clientHow to call another service from Micronaut: an interface with @Client, the service address in settings, and Optional for a 404 response.
- Testing with @MicronautTestMicronaut tests with JUnit: the @MicronautTest annotation, an HTTP client to call your own API, injecting beans into a test and replacing a bean with @MockBean.
- Security basicsMicronaut Security: adding the module, checking the login and password in an HttpRequestAuthenticationProvider, @Secured access rules and roles.
- Token login with JWTHow Micronaut Security issues a JWT through /login and accepts it in the Authorization: Bearer header, and where to keep the signing secret.
- Scheduled jobsThe @Scheduled annotation in Micronaut: running a method at an interval (fixedDelay, fixedRate), by a cron expression and with an interval from settings.
- Management endpoints and /healthThe micronaut-management module: the /health check, /info details, sensitive endpoints and how they are protected.
5.Building and running
- Building and running a jarHow to package a Micronaut application into a single executable jar, run it with java -jar and pass server settings through environment variables.
- GraalVM native imageWhat a GraalVM native image is, why Micronaut suits it well, how to build one with -Dpackaging=native-image and what it costs.