Building an Application with Micronaut
Hello everyone, today we will learn how to create a Micronaut based project via Micronaut Launch.
Micronaut is a modern, JVM-predicated, full-stack Java framework designed for building modular, facilely testable JVM applications with support for Java, Kotlin and the Groovy language.
Micronaut provides:
- Dependency Injection and Inversion of Control (IoC)
- Aspect-Oriented Programming (AOP)
- Sensible Defaults and Auto-ConfigurationMessage-Driven Applications
- HTTP Routing
- Client-Side Load Balancing
- Command-Line Applications
- HTTP ServersDistributed Configuration
- Service Discovery
- Fast startup time
- Reduced memory footprint
- Minimal use of reflection
- Minimal use of proxies
- No runtime bytecode generation
- Easy Unit Testing
What We Will build today?
We will build a simple web hello world application with Micronaut.
Technologies used:
- Java 11
- Micronaut 3.1.1
- Maven 3.6.3
- IntelliJ IDEA
Starting with Micronaut Launch
- Navigate to https://micronaut.io/launch/ This service pulls in all the dependencies you need for an application and does most of the setup for you.
- Choose Application Type - Micronaut Application
- Choose Java Version - Java 11
- Enter the Application Name - helloworld
- Enter the Base package - org.knf.dev.demo
- Choose Micronaut Version - 3.1.1
- Choose Language - Java
- Choose either Gradle or Maven or Gradle or Kotlin - Maven
- Choose Test Framework - JUnit
- Click Generate
- Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.
- Open the Project in your favourite IDE, I am using IntelliJ IDEA
Project Directory:
Dependency management:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.knf.dev.demo</groupId>
<artifactId>micronaut-helloworld</artifactId>
<version>0.1</version>
<packaging>${packaging}</packaging>
<parent>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-parent</artifactId>
<version>3.1.1</version>
</parent>
<properties>
<packaging>jar</packaging>
<jdk.version>11</jdk.version>
<release.version>11</release.version>
<micronaut.version>3.1.1</micronaut.version>
<exec.mainClass>org.knf.dev.demo.Application</exec.mainClass>
<micronaut.runtime>netty</micronaut.runtime>
</properties>
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-runtime</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- Uncomment to enable incremental compilation -->
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->
<annotationProcessorPaths combine.children="append">
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-validation</artifactId>
<version>${micronaut.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amicronaut.processing.group=org.knf.dev.demo</arg>
<arg>-Amicronaut.processing.module=helloworld</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Controller:
package org.knf.dev.demo.controller;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;
@Controller("/echo")
public class HelloWorld {
@Get
@Produces(MediaType.APPLICATION_JSON)
public String index() {
return "Hello World";
}
}
Application:
package org.knf.dev.demo;
import io.micronaut.runtime.Micronaut;
public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}
Execute the application in development mode:
mvn mn:run
__ __ _ _
| \/ (_) ___ _ __ ___ _ __ __ _ _ _| |_
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| | | | | (__| | | (_) | | | | (_| | |_| | |_
|_| |_|_|\___|_| \___/|_| |_|\__,_|\__,_|\__|
Micronaut (v3.1.1)
00:11:49.785 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 4546ms. Server Running: http://localhost:8080