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

相关文章
相关标签/搜索