Hello World

欢迎来到Vert.x的世界,相信您在接触Vert.x的同时,迫不及待想动手试一试,如您在学习计算机其它知识一样,总是从Hello World开始,下面我们将引导您制作一个最基本简单的Hello World例子,但在此之前,我们需要您具备有以下基础知识:

  1. Java基础知识,您不需要了解Java EE或者是Java ME的知识,但是需要您对Java有所了解,在此文档中,我们不会介绍任何关于Java SE又称Core Java的知识点。请注意:Vert.x 3以上版本需要Java 8以上版本方能运行;

  2. Maven相关知识,您需要知道什么Maven是做什么用的,以及如何使用Maven;

  3. 互联网的基础知识,知道什么是网络协议,尤其是TCP,HTTP协议。

本文将会建立一个基本的HTTP服务器,并监听8080端口,对于任何发往该服务器以及端口的请求,服务器会返回一个Hello World字符串。

首先新建一个Maven项目,一个基本的Maven项目目录结构如下所示:

├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ └── resources
│ └── test
│ └── java

随后在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>io.example</groupId>
<artifactId>vertx-example</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<vertx.version>3.4.2</vertx.version>
<main.class>io.example.Main</main.class>
</properties> <dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${vertx.version}</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>${main.class}</Main-Class>
</manifestEntries>
</transformer>
</transformers>
<artifactSet />
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-prod.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

跟其它Maven项目一样,我们首先定义了项目的GroupId,ArtifactId以及版本号,随后我们定义了两个属性,分别是:vertx.version,也就是Vert.x的版本号,此处我们使用最新的Vert.x版本,也就是3.4.2;以及main.class,也就是我们要使用的包含有main函数的主类。之后我们引入了两个Maven插件,分别是maven-compiler-pluginmaven-shade-plugin,前者用来将.java的源文件编译成.class的字节码文件,后者可将编译后的.class字节码文件打包成可执行的jar文件,俗称fat-jar

然后我们在src/main/java/io/example目录下新建两个java文件,分别是Main.javaMyFirstVerticle.java,代码如下:

Main.java

package io.example;

import io.vertx.core.Vertx;

/**
* Created by chengen on 26/04/2017.
*/
public class Main {
public static void main(String[] args){
Vertx vertx = Vertx.vertx(); vertx.deployVerticle(MyFirstVerticle.class.getName());
}
}

MyFirstVerticle.java

package io.example;

import io.vertx.core.AbstractVerticle;

/**
* Created by chengen on 26/04/2017.
*/
public class MyFirstVerticle extends AbstractVerticle {
public void start() {
vertx.createHttpServer().requestHandler(req -> {
req.response()
.putHeader("content-type", "text/plain")
.end("Hello World!");
}).listen(8080);
}
}

然后用Maven的mvn package命令打包,随后在src的同级目录下会出现target目录,进入之后,会出现vert-example-1.0-SNAPSHOT.jarvert-example-1.0-SNAPSHOT-prod.jar两个jar文件,后者是可执行文件,在有图形界面的操作系统中,您可双击执行,或者用以下命令:java -jar vert-example-1.0-SNAPSHOT-prod.jar执行。

随后打开浏览器,在浏览器的地址栏中输入:http://localhost:8080/ 便可看到熟悉的Hello World!啦。

启动器

我们也可以使用Launcher来替代Main类,这也是官方推荐的方式,在pom.xml中加入main.verticle属性,并将该属性值设置为maven-shade-plugin插件的manifestEntriesMain-Verticle对应的值,最后修改main.classio.vertx.core.Launcher,修改后的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>io.example</groupId>
<artifactId>vertx-example</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<vertx.version>3.4.2</vertx.version>
<main.class>io.vertx.core.Launcher</main.class>
<main.verticle>io.example.MainVerticle</main.verticle>
</properties> <dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${vertx.version}</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>${main.class}</Main-Class>
<Main-Verticle>${main.verticle}</Main-Verticle>
</manifestEntries>
</transformer>
</transformers>
<artifactSet />
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-prod.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

然后在src/main/java/io/example目录下新增MainVerticle.java文件,代码如下:

package io.example;

import io.vertx.core.AbstractVerticle;

/**
* Created by chengen on 26/04/2017.
*/
public class MainVerticle extends AbstractVerticle {
public void start() {
vertx.deployVerticle(MyFirstVerticle.class.getName());
}
}

然后重新打包后执行,便可再次看到Hello World!。

请注意:重新打包之前,您可能需要清除之前编译后留下的文件,用mvn clean package命令打包。

测试

下面我们将会介绍测试部分,首先引入两个新的测试依赖,修改后的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>io.example</groupId>
<artifactId>vertx-example</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<vertx.version>3.4.2</vertx.version>
<main.class>io.vertx.core.Launcher</main.class>
<main.verticle>io.example.MainVerticle</main.verticle>
</properties> <dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${vertx.version}</version>
</dependency> <dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-unit</artifactId>
<version>${vertx.version}</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>${main.class}</Main-Class>
<Main-Verticle>${main.verticle}</Main-Verticle>
</manifestEntries>
</transformer> <!--多语言支持在打包时需加入以下转换器-->
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource>
</transformer> </transformers>
<artifactSet />
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-prod.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

随后在src/test/java/io/example目录下新增MyFirstVerticleTest.java文件:

package io.example;

import io.vertx.core.Vertx;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith; /**
* Created by chengen on 26/04/2017.
*/
@RunWith(VertxUnitRunner.class)
public class MyFirstVerticleTest { private Vertx vertx; @Before
public void setUp(TestContext context) {
vertx = Vertx.vertx();
vertx.deployVerticle(MyFirstVerticle.class.getName(), context.asyncAssertSuccess());
} @After
public void tearDown(TestContext context) {
vertx.close(context.asyncAssertSuccess());
} @Test
public void testApplication(TestContext context) {
final Async async = context.async(); vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {
response.handler(body -> {
context.assertTrue(body.toString().contains("Hello"));
async.complete();
});
});
}
}
package io.example;

import io.vertx.core.Vertx;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith; /**
* Created by chengen on 26/04/2017.
*/
@RunWith(VertxUnitRunner.class)
public class MyFirstVerticleTest { private Vertx vertx; @Before
public void setUp(TestContext context) {
vertx = Vertx.vertx();
vertx.deployVerticle(MyFirstVerticle.class.getName(), context.asyncAssertSuccess());
} @After
public void tearDown(TestContext context) {
vertx.close(context.asyncAssertSuccess());
} @Test
public void testApplication(TestContext context) {
final Async async = context.async(); vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {
response.handler(body -> {
context.assertTrue(body.toString().contains("Hello"));
async.complete();
});
});
}
}

执行该测试案例便可得到期望的结果,理解测试代码并不难,留给读者作为练习。

至此,大功告成,欢迎来到Vert.x的世界。

Vert.x 之 HelloWorld的更多相关文章

  1. vert.x学习(八),用JDBCClient配合c3p0操作数据库

    今天学习了下vert.x的JDBCClient,我这里将今天的学习笔记记录下来.这次学习中使用了c3p0. 用使用JDBCClient和c3p0得现在pom.xml文件里面导入对应的依赖,下面贴出xm ...

  2. vert.x学习(七),使用表单获取用户提交的数据

    在web开发中,用的最多的就是表单了,用户通过表单提交数据到系统后台,系统又可以通过表单传递的数据做业务分析.那么这章就学习在vert.x中怎么使用表单,获取表单的参数值. 编写一个表单模板代码res ...

  3. vert.x学习(五),用StaticHandler来处理静态文件

    做web开发,css.js.图片等静态资源是必不可少的,那么vert.x又是怎么来加载这些静态资源呢.请看StaticHandler 编写HelloStaticResource.java packag ...

  4. vert.x学习(四),使用模板解析器ClassLoaderTemplateResolver

    在vert.x中使用模板解析,可以为我们带来很多方便.我这里学习了一下ClassLoaderTemplateResolver的简单使用.这次工程配置与上篇一样,不需要做任何多的配置.直接编写代码就可以 ...

  5. vert.x学习(三),Web开发之Thymeleaf模板的使用

    在vert.x中使用Thymeleaf模板,需要引入vertx-web-templ-thymeleaf依赖.pom.xml文件如下 <?xml version="1.0" e ...

  6. vert.x学习(一),开篇之hello world

    今天决定学习下vert.x这个框架,记录下学习笔记. 下面列下我的开发环境: Java版本 1.8 maven版本 3.3 IDEA版本 2016 在idea中使用vert.x不用下载或安装其他东西了 ...

  7. 小强学渲染之Unity Shader编程HelloWorld

    第一个简单的顶点vert/片元frag着色器   1)打开Unity 5.6编辑器,新建一个场景后ctrl+s保存命名为Scene_5.默认创建的场景是包含了一摄像机,一平行光,且场景背景是一天空盒而 ...

  8. 使用webstorm+webpack构建简单入门级“HelloWorld”的应用&&引用jquery来实现alert

    使用webstorm+webpack构建简单入门级"HelloWorld"的应用&&构建使用jquery来实现 1.首先你自己把webstorm安装完成. 请参考这 ...

  9. Idea下用SBT搭建Spark Helloworld

    没用过IDEA工具,听说跟Eclipse差不多,sbt在Idea其实就等于maven在Eclipse.Spark运行在JVM中,所以要在Idea下运行spark,就先要安装JDK 1.8+ 然后加入S ...

随机推荐

  1. vue-cli项目下引入vant组件

    前言 Vant是有赞前端团队基于有赞统一的规范实现的 Vue 组件库,提供了一整套 UI 基础组件和业务组件.通过 Vant,可以快速搭建出风格统一的页面,提升开发效率.目前已有近50个组件,这些组件 ...

  2. Kali-Linux-美化与优化

    照理说,linux的桌面是不应当存在在这个世界上的,作为一个linux用户,一味捣鼓桌面显得hin-不专业.但是,虚拟机要用到,浏览器要用到--更何况,自己的老婆能不打扮一下么? update:201 ...

  3. js 设计模式&&query

    1. 语法: try{           //需要执行的代码      }catch(e){           //错误处理 e程序遇到错误时的报错信息      } 2.惰性函数: 函数在第一次 ...

  4. NuGet的安装和使用

    好久没有用NuGet了.今天项目中正好有需要.因长时间不用,所以还要去网上看攻略,索性记录下来免得再出现类似情况.(我是一个比较懒得人,不喜欢写博客园,平时都随手整理到本地PC上.以后要努力改掉这个坏 ...

  5. Spring学习之旅(六)--SpringMVC集成

    对大多数 Java 开发来说,基于 web 的应用程序是我们主要的关注点. Spring 也提供了对于 web 的支持,基于 MVC 模式的 Spring MVC 能够帮助我们灵活和松耦合的完成 we ...

  6. 2013-2014 ACM-ICPC Pacific Northwest Regional Contest L.Languages

    题意略. 思路: 这种字符串的模拟题,应该熟练使用stringstream. 详见代码: #include<bits/stdc++.h> using namespace std; map& ...

  7. 转载-Springboot整合ehcache缓存

    转载:https://www.cnblogs.com/xzmiyx/p/9897623.html EhCache是一个比较成熟的Java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统 ...

  8. div拖拽

    分析逻辑关于该过程有一下3个动作 1.点击 2.移动 3.释放鼠标 1.点击时获得点击下去的一点的坐标(盒子的top,left),去除默认事件. 2.移动时不断改变盒子的坐标.(移动的dom目标应该为 ...

  9. Linux中安装PostgreSQL-10.1

    环境说明 Linux版本:CentOS Linux release 7.6.1810 (Core) PostgreSQL版本:PostgreSQL-10.1 PostgreSQL下载网址:https: ...

  10. LR模型常见问题

    信息速览 基础知识介绍-广义线性回归 逻辑斯蒂回归模型推导 逻辑斯蒂回归常见问题 补充知识信息点 基础知识: 机器学习对结果的形式分类: 分类算法 回归算法 LR:logistic regressio ...