Hello World with Micronaut

Micronaut?

Micronaut

Road to Micronaut 1.0-JVM-based full stack framework

Recently, I feel like I've been hearing the names here and there, so let's give it a try.

environment

The usage environment this time is here.

$ java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

CLI installation

Creating an application seems to start with the mn command (CLI).

Download from the Download Page or install with SDKMAN.

This time, let's install using SDKMAN.

Build/Install the CLI

$ sdk install micronaut

This time, Micronaut 1.0.4 is installed.

$ mn -V
| Micronaut Version: 1.0.4
| JVM Version: 1.8.0_191

Application template

Now, let's create an application template using the mn command.

Creating a Server Application

Gradle seems to be used for the project created by mn create-app, but Apache Maven is personally good, so specify --build maven to use Maven.

$ mn create-app hello-world --build maven

This will create the hello-world directory.

$ cd hello-world

Let's list the generated files.

$ tree
.
├── Dockerfile
├── micronaut-cli.yml
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── hello
    │   │       └── world
    │   │           └── Application.java
    │   └── resources
    │       ├── application.yml
    │       └── logback.xml
    └── test
        └── java
            └── hello
                └── world

10 directories, 8 files

I have a project using Maven Wrapper.

You can even create a Dockerfile.

Looking at the generated pom.xml, the main class, maven-shade-plugin and ʻexec-maven-plugin` are set, and it seems that it can be used as it is.

Click here for a class that has a main method.

src/main/java/hello/world/Application.java

package hello.world;

import io.micronaut.runtime.Micronaut;

public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class);
    }
}

Build and packaging.

$ ./mvnw package

Now that the JAR file has been created, let's start it.

$ java -jar target/hello-world-0.1.jar 
12:56:59.043 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 1108ms. Server Running: http://localhost:8080

Start up in 1 second!

Try accessing with curl.

$ curl localhost:8080
{"_links":{"self":{"href":"/","templated":false}},"message":"Page Not Found"}

Not Found, but a response was returned.

Now, let's learn from the document and create a Controller.

src/main/java/hello/world/HelloController.java

package hello.world;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;

@Controller("/hello") 
public class HelloController {
    @Get(produces = MediaType.TEXT_PLAIN) 
    public String index() {
        return "Hello World"; 
    }
}

Build and start.

$ ./mvnw package
$ java -jar target/hello-world-0.1.jar

Verification.

$ curl localhost:8080/hello
Hello World

It worked.

When developing with IDE

When developing with IntelliJ IDEA, it seems that Annotation Processors need to be enabled.

Setting up an IDE

The atmosphere is like this.

When developing with Groovy or Kotlin

It seems to adjust with --features at the time of mvn create-app.

$ mn create-app hello-world-groovy --build maven --features=groovy

$ mn create-app hello-world-kotlin --build maven --features=kotlin

About other generated files

I'll put it on.

pom.xml

<?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>hello.world</groupId>
  <artifactId>hello-world</artifactId>
  <version>0.1</version>
  <properties>
    <micronaut.version>1.0.4</micronaut.version>
    <jdk.version>1.8</jdk.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <exec.mainClass>hello.world.Application</exec.mainClass>
  </properties>
  <repositories>
    <repository>
      <id>jcenter.bintray.com</id>
      <url>https://jcenter.bintray.com</url>
    </repository>
  </repositories>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>io.micronaut</groupId>
        <artifactId>micronaut-bom</artifactId>
        <version>${micronaut.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <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>io.micronaut</groupId>
      <artifactId>micronaut-runtime</artifactId>
      <scope>compile</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-inject-java</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-all</artifactId>
      <version>1.3</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>${exec.mainClass}</mainClass>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <configuration>
          <executable>java</executable>
          <arguments>
            <argument>-noverify</argument>
            <argument>-XX:TieredStopAtLevel=1</argument>
            <argument>-classpath</argument>
            <classpath/>
            <argument>${exec.mainClass}</argument>
          </arguments>
        </configuration>
      </plugin>
    </plugins>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
          <configuration>
            <source>${jdk.version}</source>
            <target>${jdk.version}</target>
            <encoding>UTF-8</encoding>
            <compilerArgs>
              <arg>-parameters</arg>
            </compilerArgs>
            <annotationProcessorPaths>
                  <path>
                    <groupId>io.micronaut</groupId>
                    <artifactId>micronaut-inject-java</artifactId>
                    <version>${micronaut.version}</version>
                  </path>
                  <path>
                    <groupId>io.micronaut</groupId>
                    <artifactId>micronaut-validation</artifactId>
                    <version>${micronaut.version}</version>
                  </path>
            </annotationProcessorPaths>
          </configuration>
          <executions>
            <execution>
              <id>test-compile</id>
              <goals>
                <goal>testCompile</goal>
              </goals>
              <configuration>
                <compilerArgs>
                  <arg>-parameters</arg>
                </compilerArgs>
                <annotationProcessorPaths>
                  <path>
                    <groupId>io.micronaut</groupId>
                    <artifactId>micronaut-inject-java</artifactId>
                    <version>${micronaut.version}</version>
                  </path>
                  <path>
                    <groupId>io.micronaut</groupId>
                    <artifactId>micronaut-validation</artifactId>
                    <version>${micronaut.version}</version>
                  </path>
                </annotationProcessorPaths>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Dockerfile

FROM openjdk:8u171-alpine3.7
RUN apk --no-cache add curl
COPY target/hello-world*.jar hello-world.jar
CMD java ${JAVA_OPTS} -jar hello-world.jar

micronaut-cli.yml

profile: service
defaultPackage: hello.world
---
testFramework: junit
sourceLanguage: java

src/main/resources/application.yml

micronaut:
    application:
        name: hello-world

src/main/resources/logback.xml

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Recommended Posts

Hello World with Micronaut
Hello World with Spring Boot
Hello World with Spring Boot!
Hello World with VS Code!
Hello World with Spring Boot
Hello World with SpringBoot / Gradle
Hello, World! With Asakusa Framework!
Hello world with Kotlin and JavaFX
(Intellij) Hello World with Spring Boot
Create PDF with itext7 ~ Hello World ~
"Hello world" for ImageJ with Eclipse
Read "Hello world"
Java, Hello, world!
Java Hello World
Hello World with Eclipse + Spring Boot + Maven
Hello world with Java template engine Thymeleaf
Java development with Codenvy: Hello World! Run
"Hello, World!" With Kotlin + CLI in 5 minutes
How Spring Security works with Hello World
(IntelliJ + gradle) Hello World with Spring Boot
Try DI with Micronaut
"Hello World" in Java
Hello World with JavaFX 11 (OpenJFX) in Liberica JDK 11
Java Learning (1)-Hello World
Hello world! With Spring Boot (Marven + text editor)
Read System.out.println ("hello, world")
Let's write Hello World
Hello world in node.js
Run JSP Hello World with Tomcat on Docker
Hello World in Java
Studying Java-Part 1-Hello World
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Show a simple Hello World with SpringBoot + IntelliJ
Try to display hello world with spring + gradle
Hello World on WebAssembly
Serverless Function with Micronaut
Easy to display hello world with Rails + Docker
Hello World (REST API) with Apache Camel + Spring Boot 2
Hello World (console app) with Apache Camel + Spring Boot 2
[Vue Rails] "Hello Vue!" Displayed with Vue + Rails
Access Apache Kafka with Micronaut
java hello world, compile, run
Java beginners read Hello World
[Java] One type of alphabet prohibited With binding Hello, world! [Binding]
Hello World with Ruby extension library for the time being
Create a Hello World web app with Spring framework + Jetty
Spring Boot Hello World in Eclipse
Write a Reactive server with Micronaut
[Swift] Create a project with Xcode (ver 12.1) and display "Hello, World!"
Hello World for ImageJ Java Plugin
Until you run Hello World of JavaFX with VS Code + Gradle
Try Hello World with the minimum configuration of Heroku Java spring-boot
Hello World on AWS Lambda + Java
Add Bean Validation with Micronaut (Java)
Hello World in java in eclipse now
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
hello, world in Vanilla Java-EHW2018 "MVP"
Android OS (7.1.2) build and Hello World
Hello world in Java and Gradle
Spring5 MVC Web App Development with Visual Studio Code Hello World Creation
Spring Boot2 Web application development with Visual Studio Code Hello World creation