如何在 Vert.x rest api 中使用球衣、静态资源和 sockjs 在一个端口上暴露事件

How to serve in Vert.x rest api using jersey, static resources and sockjs exposed event on one port

我可以 运行 in Vert.x : vertx-jersey for rest api and Vert.x Web with SockJSHandler on one port?

在结果中,我希望 "localhost:8080/api" 用于休息 api,"localhost:8080/ebus" 用于 sockjs 公开消息,"localhost:8080/" 用于服务 javascript 前端.

不仅你可以,这是通常的做法。

    final Pattern chatUrlPattern = Pattern.compile("/ebus/(\w+)");
    final EventBus eventBus = this.vertx.eventBus();

    final Router router = Router.router(this.vertx);

    // Or wherever your static content is
    router.route("web/*").handler(StaticHandler.create());        

    this.vertx.createHttpServer().websocketHandler(ws -> {
        final Matcher m = chatUrlPattern.matcher(ws.path());
        if (!m.matches()) {
            ws.reject();
            return;
        }
        /* Your code here */
    }

    // Your Jersey code here, for example:
    vertx.runOnContext(aVoid -> {

        // Set up the jersey configuration
        // The minimum config required is a package to inspect for JAX-RS endpoints
        vertx.getOrCreateContext().config()
                .put("jersey", new JsonObject()
                        .put("port", 8080)
                        .put("packages", new JsonArray()
                                .add(HelloWorldEndpoint.class.getPackage().getName())));

        // Use a service locator (HK2 or Guice are supported by default) to create the jersey server
        ServiceLocator locator = ServiceLocatorUtilities.bind(new HK2JerseyBinder(), new HK2VertxBinder(vertx));
        JerseyServer server = locator.getService(JerseyServer.class);

        // Start the server which simply returns "Hello World!" to each GET request.
        server.start();

    });