I am extremely new to Vert.x, like a couple of days new. I come from a JAX-RS, RESTeasy world. I might be extremely wrong, please correct me.
So, I want to write a REST API using vertx-web and Spring. I see Verticles as REST resources. I took a look on vertx-web blog and spring-example, but the examples are pretty simple, and mostly comprised of only one resource and verticle.
My question is: How do you make a Verticle expose its own REST interface (sub-router), and how do you register its sub-router into application's main-router?
I have tried something like but i get 404 when I request /products/all :(
public class ProductsVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> startFuture) throws Exception {
super.start(startFuture);
}
public static Router getRouter() {
Router router = Router.router(Vertx.vertx());
router.get("/all").consumes("application/json").produces("application/json")
.handler(routingContext -> {
routingContext.response()
.putHeader("content-type", "text/html")
.end("<h1>Products</h1>");
});
return router;
}
}
public class ServerVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
super.start();
Router mainRouter = Router.router(vertx);
ProductsVerticle productsVerticle = new ProductsVerticle();
vertx.deployVerticle(productsVerticle, handler -> {
if (handler.succeeded()) {
LOG.info("Products Verticle deployed successfully");
mainRouter.mountSubRouter("/products", productsVerticle.getRouter());
}
});
mainRouter.get("/static").handler(routingContext -> {
routingContext.response()
.putHeader("content-type", "text/html")
.end("<h1>Hello from my first Vert.x 3 application</h1>");
});
HttpServer server = vertx.createHttpServer();
server.requestHandler(mainRouter::accept);
server.listen(8090);
}
}