Vertx搭建Web服務器

  1. 首先引用Vertx的Web包

    <dependency>
     <groupId>io.vertx</groupId>
    <artifactId>vertx-web</artifactId>
    <version>3.4.1</version>
    </dependency>
    
  2. 新建VertxWeb()類,繼承AbstractVerticle類,註冊使用Vertx,重載start方法

    public static Vertx vertx;
    public static void main(String[] args) {
     vertx = Vertx.vertx();
    // 部署發佈rest服務
     vertx.deployVerticle(new VertxWeb());
    }
    @Override
    public void start() throws Exception {
        httpServer = vertx.createHttpServer();
        final Router router = Router.router(vertx);
        router.route().handler(CorsHandler.create("*")
                .allowedMethod(HttpMethod.GET)
                .allowedMethod(HttpMethod.POST)
                .allowedMethod(HttpMethod.OPTIONS)
                .allowedHeader("X-PINGARUNER")
                .allowedHeader("Content-Type"));
        router.route().handler(BodyHandler.create());
        router.post("/searchResult").handler(this::searchResult);
        httpServer.requestHandler(router::accept).listen(8080);
    }
    private void searchResult(RoutingContext context) {
        context.response().end("ok");
        System.out.println(context.getBodyAsString());
    }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章