[JAVA] Hallo Welt mit Micronaut

Micronaut?

Micronaut

Road to Micronaut 1.0-JVM-basiertes Full-Stack-Framework

Vor kurzem habe ich das Gefühl, hier und da die Namen gehört zu haben, also probieren wir es aus.

Umgebung

Die Nutzungsumgebung ist diesmal hier.

$ 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

Das Erstellen einer Anwendung scheint mit dem Befehl mn (CLI) zu beginnen.

Laden Sie es von der Download-Seite herunter oder installieren Sie es mit SDKMAN.

Dieses Mal installieren wir mit SDKMAN.

Build/Install the CLI

$ sdk install micronaut

Dieses Mal wurde Micronaut 1.0.4 installiert.

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

Erstellen einer Anwendungsvorlage

Jetzt erstellen wir eine Vorlage für die Anwendung mit dem Befehl mn.

Creating a Server Application

Gradle scheint für das von mn create-app erstellte Projekt verwendet zu werden, aber ich persönlich bevorzuge Apache Maven. Geben Sie also --build maven an, um Maven zu verwenden.

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

Dadurch wird das Verzeichnis "Hallo Welt" erstellt.

$ cd hello-world

Lassen Sie uns die generierten Dateien auflisten.

$ 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

Ich habe ein Projekt mit Maven Wrapper.

Sie können sogar eine Docker-Datei erstellen.

Wenn man sich die generierte "pom.xml" ansieht, sind die Hauptklasse "Maven-Shade-Plugin" und "Exec-Maven-Plugin" festgelegt, und es scheint, dass sie so verwendet werden kann, wie sie ist.

Klicken Sie hier für eine Klasse mit einer Hauptmethode.

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);
    }
}

Bauen, verpacken.

$ ./mvnw package

Nachdem die JAR-Datei erstellt wurde, starten wir sie.

$ 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

In 1 Sekunde starten!

Versuchen Sie den Zugriff mit "Curl".

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

Nicht gefunden, aber eine Antwort wurde zurückgegeben.

Lassen Sie uns nun aus dem Dokument lernen und einen Controller erstellen.

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"; 
    }
}

Bauen und starten.

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

Bestätigung.

$ curl localhost:8080/hello
Hello World

Es funktionierte.

Bei der Entwicklung mit IDE

Bei der Entwicklung mit IntelliJ IDEA müssen anscheinend Anmerkungsprozessoren aktiviert werden.

Setting up an IDE

Die Atmosphäre ist so.

Bei der Entwicklung mit Groovy oder Kotlin

Zum Zeitpunkt von mvn create-app scheint es sich mit --features anzupassen.

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

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

Über andere generierte Dateien

Ich werde es anziehen.

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

Hallo Welt mit Micronaut
Hallo Welt mit Spring Boot
Hallo Welt mit Spring Boot!
Hallo Welt mit VS Code!
Hallo Welt mit Spring Boot
Hallo Welt mit SpringBoot / Gradle
Hallo Welt, mit Asakusa Framework!
Hallo Welt mit Kotlin und JavaFX
(Intellij) Hallo Welt mit Spring Boot
Erstellen Sie PDF mit itext7 ~ HelloWorld ~
"Hallo Welt" für ImageJ mit Eclipse
Lesen Sie "Hallo Welt"
Java, Hallo Welt!
Java Hallo Welt
Hallo Welt mit Eclipse + Spring Boot + Maven
Hallo Welt mit Java Template Engine Thymeleaf
Java-Entwicklung mit Codenvy: Hello World! Run
"Hallo Welt!" Mit Kotlin + CLI in 5 Minuten
Wie Spring Security mit Hello World funktioniert
(IntelliJ + gradle) Hallo Welt mit Spring Boot
Versuchen Sie DI mit Micronaut
"Hallo Welt" in Java
Hallo Welt mit JavaFX 11 (OpenJFX) von Liberica JDK 11
Java lernen (1) -Hallo Welt
Hallo Welt! Mit Spring Boot (Marven + Texteditor)
Lesen Sie System.out.println ("Hallo Welt")
Schreiben wir Hello World
Führen Sie JSP Hello World mit Tomcat auf Docker aus
Hallo Welt in Java
Java-Teil 1-Hallo Welt studieren
[Java] Hallo Welt mit Java 14 x Spring Boot 2.3 x JUnit 5 ~
Zeigen Sie eine einfache Hallo Welt mit SpringBoot + IntelliJ
Versuchen Sie, Hallo Welt mit Frühling + Gradle anzuzeigen
Hallo Welt mit Web Assembly
Serverlose Funktion mit Micronaut
Mit Rails + Docker einfach Hallo Welt anzuzeigen
Hallo Welt (REST API) mit Apache Camel + Spring Boot 2
Hallo Welt (Konsolen-App) mit Apache Camel + Spring Boot 2
[Vue Rails] "Hallo Vue!" Wird mit Vue + Rails angezeigt
Greifen Sie mit Micronaut auf Apache Kafka zu
Java Hallo Welt, kompilieren, ausführen
Java-Anfänger lesen Hello World
[Java] Eine Art von Alphabet verboten Mit Bindung Hallo, Welt! [Bindung]
Hallo Welt mit Ruby-Erweiterungsbibliothek vorerst
Erstellen Sie eine Hello World-Webanwendung mit Spring Framework + Jetty
Frühlingsstiefel Hallo Welt in Eclipse
Schreiben Sie einen reaktiven Server mit Micronaut
[Swift] Erstellen Sie ein Projekt mit Xcode (Version 12.1) und zeigen Sie "Hallo Welt!"
Hallo Welt für ImageJ Java Plugin
Bis Sie Hello World of JavaFX mit VSCode + Gradle ausführen
Probieren Sie HelloWorld mit der Mindestkonfiguration von Heroku Java Spring-Boot aus
Hallo Welt mit AWS Lambda + Java
Bean Validation mit Micronaut (Java) hinzufügen
Hallo Welt von Java in Eclipse jetzt
Vergleiche Hallo Welt! Mit Spring Boot mit Java, Kotlin und Groovy
Hallo, Welt in Vanille Java-EHW2018 "MVP"
Android OS (7.1.2) Build und Hello World
Hallo Welt in Java und Gradle
Spring5 MVC-Webanwendungsentwicklung mit Visual Studio-Code Hello World Creation
Spring Boot2-Webanwendungsentwicklung mit Visual Studio Code Hello World-Erstellung