4

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);
}

}

user3159152
  • 611
  • 4
  • 12
  • 20

2 Answers2

2

Your need is absolutly understandable. But we should maybe shortly think about what spring does: - when Application server starts up a startuphook is executed which searches the whole classpath for every class anotated with Jax-rs Annotations and initalizeses them or just registers them on a "router".

So if you want that, you can have that but you have to do that by ur self. Im sorry :D.

e.g:

class Server extends AbstractVerticle {

    @Override
    public void start() throws Exception {
        List<AbstractVerticle> verticles = searchForVerticlesWithMyAnnotation();
        verticles.forEach((V) = > router.add(V));
    }

}

@MyOwnJax(path = "/blaa")
public class TestService {
}

@interface MyOwnJax {
    String path();
}

The method "searchForVerticlesWIthMyAnnotation" is the tricky thing here. It should not be to slow. But if you use Spring anyway you can use something like: org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

or see here : Stackoverflow: Search for Annotations @runtime

BUT and this is a big but here. ;) Maybe you have a better idea then Spring to make your REST api ? Spring is really "klobby" in my opinion whereas Vertx.x is really smooth. (Im sorry for the not really pragmatic opinion of mine. )

I use in my application an approach with DI. Which means:

router.route(HttpMethod.GET,
 "/user/login").handler(injector.getInstance(ILoginUser.class));

With normal guice framework as injector. And while this is just an Interface you can make really big changes before you have to change something in the verticle which starts the server. (Actually mostly just if you have to add or remove a path)

Summary:

  • If you want to have a Spring approach you have to use reflection or a library which uses reflection. Downside: Startup-performance, Sometimes a bit to much magic and really hard to find errors/debug. Upside: So easy to test, really really easy to extend functionality

  • Register the Verticles on the path urself. Downside: You have to add / remove paths on the "server"-verticle. Upside: Startup-performance, No magic, full control about what happens and when.

Thats just a short summary and many points aren't mentioned. But i hope this answers your questions. If you have some follwoup questions just write!

Jeerze,

Simi

Community
  • 1
  • 1
Simi
  • 174
  • 1
  • 7
2

I have recently written a simple JAX-RS annotation lib for vert.x.
In it's approach it is similar to RestEasy.

https://github.com/zandero/rest.vertx

Drejc
  • 14,196
  • 16
  • 71
  • 106