简体   繁体   English

Vert.x RESTful Verticle

[英]Vert.x RESTful Verticle

I am extremely new to Vert.x, like a couple of days new. 我对Vert.x非常陌生,就像新的一样。 I come from a JAX-RS, RESTeasy world. 我来自JAX-RS,RESTeasy世界。 I might be extremely wrong, please correct me. 我可能是非常错的,请指正。

So, I want to write a REST API using vertx-web and Spring. 所以,我想使用vertx-web和Spring编写REST API。 I see Verticles as REST resources. 我将Verticle视为REST资源。 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. 我看了一下vertx-web博客spring-example ,但这些例子非常简单,大多只包含一个资源和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? 我的问题是 :如何使Verticle公开自己的REST接口(子路由器),以及如何将其子路由器注册到应用程序的主路由器?

I have tried something like but i get 404 when I request /products/all :( 我尝试了类似的东西,但当我要求/产品/所有时我得到404 :(

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

} }

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". 但是我们应该很快就会想到Spring会做什么: - 当应用服务器启动时,执行startuphook,它会搜索整个类路径,查找用Jax-rs Annotations分配的每个类,并将它们初始化或只是将它们注册到“路由器”上。

So if you want that, you can have that but you have to do that by ur self. 因此,如果你想要那样,你可以拥有它,但你必须通过你自己做到这一点。 Im sorry :D. 对不起:D。

eg: 例如:

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. 方法“searchForVerticlesWIthMyAnnotation”在这里是棘手的事情。 It should not be to slow. 它不应该慢。 But if you use Spring anyway you can use something like: org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider 但是如果你使用Spring,你可以使用类似的东西: org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

or see here : Stackoverflow: Search for Annotations @runtime 或者在这里看到: Stackoverflow:搜索注释@runtime

BUT and this is a big but here. ,这是一个很大的,但在这里。 ;) Maybe you have a better idea then Spring to make your REST api ? ;)也许你有一个更好的主意,然后Spring来制作你的REST api? Spring is really "klobby" in my opinion whereas Vertx.x is really smooth. 在我看来,Spring真的是“klobby”,而Vertx.x非常流畅。 (Im sorry for the not really pragmatic opinion of mine. ) (对不起我的不切实际的意见。)

I use in my application an approach with DI. 我在我的应用程序中使用DI的方法。 Which means: 意思是:

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

With normal guice framework as injector. 使用普通的guice框架作为注入器。 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. 虽然这只是一个界面,但您必须在启动服务器的Verticle中更改某些内容之前进行重大更改。 (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. 如果你想要一个Spring方法,你必须使用反射或使用反射的库。 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. 在路径上注册Verticles。 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, Jeerze,

Simi 西米

I have recently written a simple JAX-RS annotation lib for vert.x. 我最近为vert.x编写了一个简单的JAX-RS注释库。
In it's approach it is similar to RestEasy. 在它的方法中它类似于RestEasy。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM