2017-04-01 10:50:41 +02:00
|
|
|
package com.baeldung.rest;
|
2017-03-19 22:28:34 +05:30
|
|
|
|
|
|
|
|
import com.baeldung.model.Article;
|
|
|
|
|
|
|
|
|
|
import io.vertx.core.AbstractVerticle;
|
|
|
|
|
import io.vertx.core.Future;
|
|
|
|
|
import io.vertx.core.json.Json;
|
|
|
|
|
import io.vertx.ext.web.Router;
|
|
|
|
|
import io.vertx.ext.web.RoutingContext;
|
|
|
|
|
|
|
|
|
|
public class RestServiceVerticle extends AbstractVerticle {
|
|
|
|
|
@Override
|
|
|
|
|
public void start(Future<Void> future) {
|
|
|
|
|
|
|
|
|
|
Router router = Router.router(vertx);
|
|
|
|
|
router.get("/api/baeldung/articles/article/:id")
|
2017-03-20 15:50:43 +00:00
|
|
|
.handler(this::getArticles);
|
2017-03-19 22:28:34 +05:30
|
|
|
|
|
|
|
|
vertx.createHttpServer()
|
2017-03-20 15:50:43 +00:00
|
|
|
.requestHandler(router::accept)
|
|
|
|
|
.listen(config().getInteger("http.port", 8080), result -> {
|
|
|
|
|
if (result.succeeded()) {
|
|
|
|
|
future.complete();
|
|
|
|
|
} else {
|
|
|
|
|
future.fail(result.cause());
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-03-19 22:28:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void getArticles(RoutingContext routingContext) {
|
|
|
|
|
String articleId = routingContext.request()
|
2017-03-20 15:50:43 +00:00
|
|
|
.getParam("id");
|
2017-03-19 22:28:34 +05:30
|
|
|
Article article = new Article(articleId, "This is an intro to vertx", "baeldung", "01-02-2017", 1578);
|
|
|
|
|
|
|
|
|
|
routingContext.response()
|
2017-03-20 15:50:43 +00:00
|
|
|
.putHeader("content-type", "application/json")
|
|
|
|
|
.setStatusCode(200)
|
|
|
|
|
.end(Json.encodePrettily(article));
|
2017-03-19 22:28:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|