work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Vertx开发路由

Posted on 2022-02-15 23:37  work hard work smart  阅读(86)  评论(0编辑  收藏  举报

一、基本使用

1、基本路由实现

1).增加依赖包

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web</artifactId>
            <version>3.5.2</version>
        </dependency>

  

2)、创建一个HttpServer

public class SimpleRouter extends AbstractVerticle {

    @Override
    public void start() throws Exception {
       //创建HttpServer
        HttpServer server = vertx.createHttpServer();

        //创建路由对象
        Router router = Router.router(vertx);

        // 监听/index 地址
        router.route("/index").handler(request -> {
            request.response().end("INDEX SUCCESS");
        });

        // 把请求交给路由处理
        server.requestHandler(router::accept);
        server.listen(8888);
    }

    public static void main(String[] args) {
        Vertx.vertx().deployVerticle(new SimpleRouter());
    }
}

  

3)、访问http://localhost:8888/index

 

 2、限制HTTP请求方法

        router.post("/post").handler(request -> {
            request.response().end("POST SUCCESS");
        });

 

 

 

参考:https://blog.csdn.net/king_kgh/article/details/80848571