【问题标题】:How to serve in Vert.x rest api using jersey, static resources and sockjs exposed event on one port如何在一个端口上使用球衣、静态资源和 sockjs 暴露事件在 Vert.x 中提供服务
【发布时间】:2017-09-15 08:51:42
【问题描述】:

我可以在 Vert.x 中运行吗:vertx-jersey for rest api 和 Vert.x Web 与 SockJSHandler 在一个端口上?

结果我希望“localhost:8080/api”用于休息 api,“localhost:8080/ebus”用于 sockjs 公开消息,“localhost:8080/”用于服务 javascript 前端。

【问题讨论】:

    标签: jersey vert.x


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,我是要和 JerseyVerticle 堆叠的,你给我指明了方向。
    猜你喜欢
    • 2019-05-25
    • 2016-04-24
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2013-12-07
    相关资源
    最近更新 更多