Start

    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项目目录结构如下所示:

    随后在中加入相关的依赖和插件,如下所示:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <groupId>io.example</groupId>
    7. <artifactId>vertx-example</artifactId>
    8. <version>1.0-SNAPSHOT</version>
    9. <properties>
    10. <vertx.version>3.4.2</vertx.version>
    11. <main.class>io.example.Main</main.class>
    12. </properties>
    13. <dependencies>
    14. <dependency>
    15. <groupId>io.vertx</groupId>
    16. <artifactId>vertx-core</artifactId>
    17. <version>${vertx.version}</version>
    18. </dependency>
    19. </dependencies>
    20. <build>
    21. <plugins>
    22. <plugin>
    23. <artifactId>maven-compiler-plugin</artifactId>
    24. <version>3.3</version>
    25. <configuration>
    26. <source>1.8</source>
    27. <target>1.8</target>
    28. </configuration>
    29. </plugin>
    30. <plugin>
    31. <groupId>org.apache.maven.plugins</groupId>
    32. <artifactId>maven-shade-plugin</artifactId>
    33. <version>2.4.2</version>
    34. <executions>
    35. <phase>package</phase>
    36. <goals>
    37. <goal>shade</goal>
    38. </goals>
    39. <configuration>
    40. <transformers>
    41. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    42. <manifestEntries>
    43. <Main-Class>${main.class}</Main-Class>
    44. </manifestEntries>
    45. </transformer>
    46. </transformers>
    47. <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-prod.jar</outputFile>
    48. </configuration>
    49. </execution>
    50. </executions>
    51. </plugin>
    52. </plugins>
    53. </build>
    54. </project>

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

    Main.java

    MyFirstVerticle.java

    1. package io.example;
    2. import io.vertx.core.AbstractVerticle;
    3. /**
    4. * Created by chengen on 26/04/2017.
    5. */
    6. public class MyFirstVerticle extends AbstractVerticle {
    7. public void start() {
    8. vertx.createHttpServer().requestHandler(req -> {
    9. req.response()
    10. .putHeader("content-type", "text/plain")
    11. .end("Hello World!");
    12. }).listen(8080);
    13. }
    14. }

    然后用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如下:

    1. package io.example;
    2. import io.vertx.core.AbstractVerticle;
    3. * Created by chengen on 26/04/2017.
    4. */
    5. public class MainVerticle extends AbstractVerticle {
    6. vertx.deployVerticle(MyFirstVerticle.class.getName());
    7. }
    8. }

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

    下面我们将会介绍测试部分,首先引入两个新的测试依赖,修改后的pom.xml如下:

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

    1. package io.example;
    2. import io.vertx.core.Vertx;
    3. import io.vertx.ext.unit.Async;
    4. import io.vertx.ext.unit.TestContext;
    5. import io.vertx.ext.unit.junit.VertxUnitRunner;
    6. import org.junit.After;
    7. import org.junit.Before;
    8. import org.junit.Test;
    9. import org.junit.runner.RunWith;
    10. /**
    11. * Created by chengen on 26/04/2017.
    12. */
    13. @RunWith(VertxUnitRunner.class)
    14. public class MyFirstVerticleTest {
    15. private Vertx vertx;
    16. @Before
    17. public void setUp(TestContext context) {
    18. vertx = Vertx.vertx();
    19. vertx.deployVerticle(MyFirstVerticle.class.getName(), context.asyncAssertSuccess());
    20. }
    21. @After
    22. public void tearDown(TestContext context) {
    23. vertx.close(context.asyncAssertSuccess());
    24. }
    25. @Test
    26. public void testApplication(TestContext context) {
    27. final Async async = context.async();
    28. vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {
    29. response.handler(body -> {
    30. context.assertTrue(body.toString().contains("Hello"));
    31. async.complete();
    32. });
    33. });
    34. }
    35. }

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

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