Vert.x Notes: 2.hello vert.x -- The first vert.x hello world Project

Keywords: Maven Eclipse Attribute Apache

Hypothesis:

This article and the following series assume that you already have some knowledge of functional programming and lambda anonymous functions in the new features of jdk1.8, and are skilled in using maven.

Development environment configuration:

Using the latest version of vert.x 3.0, you need to install jdk1.8
maven requires more than 3.0 versions. It is recommended to use the latest version directly.
How to configure jdk and maven, refer to Baidu Tutorial

ide requirements: MyEclipse 2015 stable 1.0 or above or eclipse 4.4 or above

The first maven project

New pom.xml file

<?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>com.heartlifes</groupId>
    <artifactId>vertx-demo</artifactId>
    <version>3.0.0</version>
    <dependencies>
        <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-core</artifactId>
        <version>${project.version}</version>
    </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
            <!-- We specify the Maven compiler plugin as we need to set it to Java 1.8 -->
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <compilerArgs>
                            <arg>-Acodetrans.output=${project.basedir}/src/main</arg>
                        </compilerArgs>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Generate eclipse project:

mvn eclipse:eclipse

So far, an empty project based on vert.x framework has been created and completed.

The first hello world code

New HelloWorld class:

package com.heartlifes.vertx.demo.hello;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;

public class HelloWorld extends AbstractVerticle {

    @Override
    public void start() throws Exception {
        Router router = Router.router(vertx);
        router.route().handler(
                routingContext -> {
                    routingContext.response()
                            .putHeader("content-type", "text/html")
                            .end("hello vert.x");
                });

        vertx.createHttpServer().requestHandler(router::accept).listen(8080);
    }

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(new HelloWorld());
    }
}

Execute the code and enter localhost:8080 in the browser to see what is returned.

So far, the first hello world project for vert.x has been built, and we will explain the role of each line of code in a later chapter.

Posted by EdN on Sat, 09 Feb 2019 20:06:18 -0800