Vert.x 3.0 初探(一) Hello World

查了好多资料,示例代码都是jdk8写的,lambda表达式对于没有研究过的同学来说估计一时半会儿也没看明白,就自己写了一个通俗易懂的示例

<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>vertx.test</groupId>
	<artifactId>myvertx</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>myvertx</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>io.vertx</groupId>
			<artifactId>vertx-core</artifactId>
			<version>3.2.1</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>io.vertx</groupId>
			<artifactId>vertx-unit</artifactId>
			<version>3.2.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>io.vertx</groupId>
			<artifactId>vertx-web</artifactId>
			<version>3.2.1</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.3</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>io.vertx.core.Starter</Main-Class>
										<Main-Verticle>vertx.test.myvertx.VertxTest</Main-Verticle>
									</manifestEntries>
								</transformer>
							</transformers>
							<artifactSet />
							<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

</project>
package vertx.test.myvertx;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
 
public class VertxTest extends AbstractVerticle {
 
    @Override
    public void start() throws Exception {
 
        final Router router = Router.router(vertx);
 
        router.route();
        router.route().handler(BodyHandler.create());
        router.get("/hello").handler(new Handler<RoutingContext>() {
        	
        	public void handle(RoutingContext event) {
        		event.response().putHeader("content-type", "text/html").end("Hello World");
        	}
        });
        // 传递方法引用,监听端口
        vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
			
			public void handle(HttpServerRequest event) {
				router.accept(event);
			}
		}).listen(8080);
        
    }
}


你可能感兴趣的:(vert.x)