下面列出了io.vertx.core.http.HttpServer#requestHandler ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void start() {
this.metricsService = MetricsService.create(vertx);
HttpServer httpServer = vertx.createHttpServer();
httpServer.requestHandler(req -> {
if (req.path().equals(HEALTH_PATH)) {
req.response().end("ok");
} else if (req.path().equals(METRICS_PATH)) {
publishMetricsSnapshot(req.response());
} else {
req.response().setStatusCode(404).end("HTTP 404");
}
});
// we don't start this verticle unless this was defined
httpServer.listen(ipPort.getPort(), ipPort.getIp(), h -> {
if (h.failed()) {
log.info("failed to start admin service", h.cause());
}
});
}
public static void main(String[] args) {
final HttpServer server = Vertx.vertx().createHttpServer();
server.requestHandler(event -> {
log.info("hendling request");
final Map<String, String> o = HashMap.of("hello", "world",
"ololo", "trololo")
.toJavaMap();
final String json = Try.of(() -> mapper.writeValueAsString(o))
.getOrElseGet(throwable -> "{}");
event.response()
.putHeader("Content-Type", "application/json")
.end(json);
});
server.listen(port, event -> log.info("listening {} port.", port));
}
@Override
@SuppressWarnings("deprecation")
// TODO: vert.x 3.8.3 does not update startListen to promise, so we keep use deprecated API now. update in newer version.
public void start(Future<Void> startFuture) {
Router mainRouter = Router.router(vertx);
mainRouter.route("/").handler(context -> {
context.response().end(context.getBody());
});
HttpServer server = vertx.createHttpServer();
server.requestHandler(mainRouter);
server.listen(0, "0.0.0.0", ar -> {
if (ar.succeeded()) {
port = ar.result().actualPort();
startFuture.complete();
return;
}
startFuture.fail(ar.cause());
});
}
public static void main(String[] args) {
System.out.println("done waiting");
Vertx vertx = Vertx.vertx();
// Deploy
DeploymentOptions options = new DeploymentOptions().setWorker(true);
ZookeeperVerticle zkv = new ZookeeperVerticle();
vertx.deployVerticle(zkv, options);
HttpServer server = vertx.createHttpServer();
server.requestHandler(request -> {
HttpServerResponse response = request.response();
response.putHeader("content-type", "application/json");
JsonObject responseJson;
synchronized (WebsiteMain.jsonObject) {
responseJson = WebsiteMain.jsonObject.copy();
}
response.end(responseJson.encodePrettily());
});
server.listen(8080);
}
public static void main(String[] args) {
String resourceBasename = "example.gettingstarted.projectionrest/configuration.conf";
final Config config = ConfigFactory.load(resourceBasename);
// create a Vertx web server
final Vertx vertx = Vertx.vertx();
final HttpServerOptions options = new HttpServerOptions().setPort(8080);
final HttpServer httpServer = vertx.createHttpServer(options);
final BinderStore binderStore = BinderStore.instance(config);
final Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
httpServer.requestHandler(router::accept);
/*
Expose endpoint to retrieve a document from the binder store
*/
router.route(HttpMethod.GET, "/summary/:product/:date").handler(routingContext -> {
final String product = routingContext.pathParams().get("product");
final String date = routingContext.pathParams().get("date");
final VertxRestServiceActionVisitor actionVisitor = new VertxRestServiceActionVisitor(routingContext);
actionVisitor.visit(RestServiceAction.retrieveSingleDocument(binderStore, "sales_summary", product + "_" + date));
});
httpServer.listen();
}
@Test
public void test() {
Vertx vertx = Vertx.vertx();
// request.response().putHeader("Content-Type", "text/plain").write("some text").end();
vertx.setPeriodic(1000, id -> {
System.out.println(id);
});
vertx.executeBlocking(promise -> {
}, asyncResult -> {
});
HttpServer server = vertx.createHttpServer();
Handler<HttpServerRequest> requestHandler = server.requestHandler();
}
public static void async_05(TestContext context, Vertx vertx, Handler<HttpServerRequest> requestHandler) {
Async async = context.async(2);
HttpServer server = vertx.createHttpServer();
server.requestHandler(requestHandler);
server.listen(8080, ar -> {
context.assertTrue(ar.succeeded());
async.countDown();
});
vertx.setTimer(1000, id -> {
async.complete();
});
// Wait until completion of the timer and the http request
async.awaitSuccess();
// Do something else
}
@Override
public void start() throws Exception {
HttpServer server = vertx.createHttpServer();
server.requestHandler(request -> {
LOG.info("Web request arrived");
if (request.path().endsWith("index.html")) {
request.response().putHeader("content-type", "text/html");
request.response().sendFile("src/main/webroot/index.html");
} else {
request.response().setChunked(true);
request.response().putHeader("content-type", "text/plain");
request.response().write("No such file!!");
request.response().setStatusCode(404);
request.response().end();
}
});
server.listen();
super.start();
}
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
HttpServer server = vertx.createHttpServer();
server.requestHandler(request -> {
HttpServerResponse response = request.response();
response.putHeader("content-type", "application/json");
JsonObject responseJson = SimpleWebsiteMain.jsonObject.copy();
response.end(responseJson.encodePrettily());
});
server.listen(8080);
}
@Override
protected TermServer createServer(TestContext context, HttpTermOptions options) {
HttpServer httpServer = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
Router router = Router.router(vertx);
Router subRouter = Router.router(vertx);
router.mountSubRouter("/sub", subRouter);
httpServer.requestHandler(router);
Async async = context.async();
httpServer.listen(8080, context.asyncAssertSuccess(s -> {
async.complete();
}));
async.awaitSuccess(20000);
return TermServer.createHttpTermServer(vertx, subRouter, options);
}
public static void async_04(TestContext context, Vertx vertx, Handler<HttpServerRequest> requestHandler) {
Async async = context.async();
HttpServer server = vertx.createHttpServer();
server.requestHandler(requestHandler);
server.listen(8080, ar -> {
context.assertTrue(ar.succeeded());
async.complete();
});
// Wait until completion
async.awaitSuccess();
// Do something else
}
static void configureAndStartTestServer(HttpServer httpServer) {
httpServer.requestHandler(request -> {
request.response().setStatusCode(200).end("OK");
});
startServer(httpServer);
}
static void configureAndStartTestServer(HttpServer httpServer) {
httpServer.requestHandler(request -> {
request.response().setStatusCode(200).end("OK");
});
startServer(httpServer);
}