MyFirstVerticle - srujanabala/springboot-couchbase GitHub Wiki
package com.test.verticle;
import java.util.LinkedHashMap; import java.util.Map;
import io.vertx.core.AbstractVerticle; import io.vertx.core.Future; import io.vertx.core.http.HttpServerResponse; import io.vertx.core.json.Json; import io.vertx.ext.web.Router; import io.vertx.ext.web.RoutingContext;
public class MyFirstVerticle extends AbstractVerticle {
private Map<Integer, String> products = new LinkedHashMap<>();
@Override
public void start(Future<Void> fut) {
createSomeData();
// Create a router object.
Router router = Router.router(vertx);
// Bind "/" to our hello message.
router.route("/").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response
.putHeader("content-type", "text/html")
.end("<h1>Hello from my first Vert.x 3 application</h1>");
});
// router.route("/assets/*").handler(StaticHandler.create("assets"));
router.get("/api/get1").handler(this::getAll);
router.get("/api/get2").handler(this::getAll);
//router.route("/api/whiskies*").handler(BodyHandler.create());
//router.post("/api/whiskies").handler(this::addOne);
//router.get("/api/whiskies/:id").handler(this::getOne);
//router.put("/api/whiskies/:id").handler(this::updateOne);
//router.delete("/api/whiskies/:id").handler(this::deleteOne);
// Create the HTTP server and pass the "accept" method to the request handler.
vertx
.createHttpServer()
.requestHandler(router::accept)
.listen(
// Retrieve the port from the configuration,
// default to 8080.
config().getInteger("http.port", 8080),
result -> {
if (result.succeeded()) {
fut.complete();
} else {
fut.fail(result.cause());
}
}
);
}
private void createSomeData() {
products.put(101, "Name1");
products.put(102, "Name2");
}
private void getAll(RoutingContext routingContext) {
// Write the HTTP response
// The response is in JSON using the utf-8 encoding
// We returns the list of bottles
routingContext.response()
.putHeader("content-type", "application/json; charset=utf-8")
.end(Json.encodePrettily(products));
}
}