vert.x筆記:2.hello vert.x--第一個vert.x hello world工程

原文及更多文章請見我的博客:http://heartlifes.comhtml

假設:

本文及如下系列文章,假設你已經對jdk1.8新特性中的函數式編程及lambda匿名函數有必定了解,並會熟練使用maven。java

開發環境配置:

使用最新版的vert.x 3.0,須要安裝jdk1.8
maven須要3.0以上版本,推薦直接使用最新版
jdk及maven如何配置,參考百度教程web

ide需求:myeclipse 2015 stable1.0及以上或者eclipse 4.4及以上shell

第一個maven工程

新建pom.xml文件apache

<?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>

生成eclipse工程:編程

mvn eclipse:eclipse

至此,一個基於vert.x框架空工程就建立完成了瀏覽器

第一個hello world代碼

新建HelloWorld類:框架

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

執行代碼,在瀏覽器中輸入localhost:8080,看看返回了什麼。eclipse

至此,第一個vert.x的hello world工程搭建完畢,咱們會在後面的章節裏解釋每行代碼的做用。maven

相關文章
相關標籤/搜索