一位名叫 Tim Fox 的开发人员曾在 VMware 的 SpringSource 部门工作。在此期间,他领导开发了 Vert.x 项目。2012 年,Tim 跳槽到了 Red Hat 并希望能够继续从事该项目的开发,所以VMware和Redhat为此吵了一段时间,后来项目转移到Eclipse基金会。
// Mount the handler for all incoming requests at every path and HTTP method router.route().handler(context -> { // Get the address of the request Stringaddress= context.request().connection().remoteAddress().toString(); // Get the query parameter "name" MultiMapqueryParams= context.queryParams(); Stringname= queryParams.contains("name") ? queryParams.get("name") : "unknown"; // Write a json response context.json( newJsonObject() .put("name", name) .put("address", address) .put("message", "Hello " + name + " connected from " + address) ); });
// Create the HTTP server vertx.createHttpServer() // Handle every request using the router .requestHandler(router) // Start listening .listen(8888) // Print the port .onSuccess(server -> System.out.println( "HTTP server started on port " + server.actualPort() ) ); } }
classMainVerticle : AbstractVerticle() { overridefunstart() { // Create a Router val router = Router.router(vertx)
// Mount the handler for all incoming requests at every path and HTTP method router.route().handler { context -> // Get the address of the request val address = context.request().connection().remoteAddress().toString() // Get the query parameter "name" val queryParams = context.queryParams() val name = queryParams.get("name") ?: "unknown" // Write a json response context.json( json { obj( "name" to name, "address" to address, "message" to "Hello $name connected from $address" ) } ) }
// Create the HTTP server vertx.createHttpServer() // Handle every request using the router .requestHandler(router) // Start listening .listen(8888) // Print the port .onSuccess { server -> println("HTTP server started on port " + server.actualPort()) } } }