天天看點

Vertx入門篇-01 - Hello VertxHello Vert.x

Hello Vert.x

1、添加POM相關依賴

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.vertx</groupId>
                <artifactId>vertx-stack-depchain</artifactId>
                <version>3.6.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-core</artifactId>
        </dependency>
    </dependencies>           

2、建立VertxHello1,啟動一個HttpServer

public class VertxHello1 {
    public static void main(String[] args) {
        // 建立一個 Http Server
        Vertx.vertx().createHttpServer().requestHandler(request -> {
            request.response()
                    .putHeader("Content-Type", "text/plain")
                    .write("Hello Vert.x!")
                    .end();
        }).listen(8080, http -> {
            if (http.succeeded()) {
                System.out.println("HTTP server started on http://localhost:8080");
            }else {
                http.cause().printStackTrace();
            }
        });
    }
}