Description
A Micronaut application written in Java.
Prerequisites
JDK 17+
Create a Micronaut app written in Java:
mn create-app zjc.example.micronaut.first.app.micronaut-first-app --build=maven --lang=javaProject Structure

Application.java
package zjc.example.micronaut.first.app;
import io.micronaut.runtime.Micronaut;
public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}Create a Controller
In order to create a microservice that responds with “Hello World” you need a controller:
package zjc.example.micronaut.first.app;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;
@Controller("/hello")
public class HelloController {
@Get
@Produces(MediaType.TEXT_PLAIN)
public String index() {
return "Hello World";
}
}
Testing the Example
./mvnw testRunning the Example
./mvnw mn:runOutput:
__ __ _ _
| \/ (_) ___ _ __ ___ _ __ __ _ _ _| |_
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| | | | | (__| | | (_) | | | | (_| | |_| | |_
|_| |_|_|\___|_| \___/|_| |_|\__,_|\__,_|\__|
18:57:04.953 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 493ms. Server Running: http://localhost:8080
Browser:
http://localhost:8080/hello
Native Executable with GraalVM
Native executable – see: http://jreact.com/index.php/quarkus-2/native-executable/
./mvnw package -Dpackaging=native-imageGraalVM Native Image: Generating 'micronaut-first-app' (executable)...
========================================================================================================================
[1/8] Initializing... (6.0s @ 0.14GB)
Java version: 21.0.1+12, vendor version: Oracle GraalVM 21.0.1+12.1
Graal compiler: optimization level: 2, target machine: x86-64-v3, PGO: ML-inferred
C compiler: gcc (linux, x86_64, 11.4.0)
Garbage collector: Serial GC (max heap size: 80% of RAM)
2 user-specific feature(s):
- com.oracle.svm.thirdparty.gson.GsonFeature
- io.micronaut.core.io.service.ServiceLoaderFeature
------------------------------------------------------------------------------------------------------------------------
Build resources:
- 11.67GB of memory (75.6% of 15.45GB system memory, determined at start)
- 8 thread(s) (100.0% of 8 available processor(s), determined at start)
Found pending operations, continuing analysis.
[2/8] Performing analysis... [******] (61.9s @ 1.03GB)
13,728 reachable types (88.8% of 15,455 total)
20,297 reachable fields (58.6% of 34,647 total)
74,222 reachable methods (63.7% of 116,560 total)
4,562 types, 494 fields, and 4,713 methods registered for reflection
64 types, 61 fields, and 57 methods registered for JNI access
4 native libraries: dl, pthread, rt, z
[3/8] Building universe... (7.2s @ 1.62GB)
[4/8] Parsing methods... [****] (20.2s @ 1.16GB)
[5/8] Inlining methods... [***] (2.3s @ 1.37GB)
[6/8] Compiling methods... [***********] (137.7s @ 1.37GB)
[7/8] Layouting methods... [***] (9.6s @ 1.52GB)
[8/8] Creating image... [***] (8.4s @ 1.38GB)
38.10MB (54.52%) for code area: 44,407 compilation units
27.51MB (39.36%) for image heap: 335,605 objects and 307 resources
4.28MB ( 6.12%) for other data
69.89MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area: Top 10 object types in image heap:
14.49MB java.base 10.62MB byte[] for code metadata
4.12MB svm.jar (Native Image) 4.57MB byte[] for java.lang.String
3.15MB java.xml 2.96MB java.lang.Class
1.33MB netty-buffer-4.1.107.Final.jar 2.34MB java.lang.String
1.11MB micronaut-inject-4.3.11.jar 904.13kB byte[] for reflection metadata
1.03MB micronaut-http-server-netty-4.3.11.jar 764.51kB byte[] for general heap data
918.08kB micronaut-http-4.3.11.jar 643.50kB com.oracle.svm.core.hub.DynamicHubCompanion
899.93kB micronaut-serde-support-2.8.2.jar 399.41kB c.o.svm.core.hub.DynamicHub$ReflectionMetadata
850.10kB reactor-core-3.5.11.jar 342.00kB java.util.HashMap$Node
765.44kB micronaut-core-4.3.11.jar 321.01kB java.lang.String[]
9.22MB for 51 more packages 3.73MB for 2916 more object types
Use '-H:+BuildReport' to create a report with more details.
------------------------------------------------------------------------------------------------------------------------
Security report:
- Binary includes Java deserialization.
- Use '--enable-sbom' to embed a Software Bill of Materials (SBOM) in the binary.
------------------------------------------------------------------------------------------------------------------------
Recommendations:
G1GC: Use the G1 GC ('--gc=G1') for improved latency and throughput.
PGO: Use Profile-Guided Optimizations ('--pgo') for improved throughput.
INIT: Adopt '--strict-image-heap' to prepare for the next GraalVM release.
HEAP: Set max heap for improved and more predictable memory usage.
CPU: Enable more CPU features with '-march=native' for improved performance.
------------------------------------------------------------------------------------------------------------------------
19.5s (7.6% of total time) in 589 GCs | Peak RSS: 3.12GB | CPU load: 6.52
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
/home/zbyszek/IdeaProjects/reactive/github/zjc-examples/micronaut/micronaut-first-app/target/micronaut-first-app (executable)
========================================================================================================================
Finished generating 'micronaut-first-app' in 4m 14s.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:22 min
[INFO] Finished at: 2024-04-04T19:17:18+02:00
[INFO] ------------------------------------------------------------------------
Running executable
./target/micronaut-first-app __ __ _ _
| \/ (_) ___ _ __ ___ _ __ __ _ _ _| |_
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| | | | | (__| | | (_) | | | | (_| | |_| | |_
|_| |_|_|\___|_| \___/|_| |_|\__,_|\__,_|\__|
19:45:42.771 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 14ms. Server Running: http://localhost:8080
http://localhost:8080/hello
Source code: https://github.com/ZbCiok/zjc-examples/tree/main/micronaut/micronaut-first-app
–
