Search code examples
vert.xvertx-httpclient

Different Handlers for URL patterns (same prefix) in vertx


I'm using vertx-4.x. I've sample url patterns like this:

api/v1/xyz_development/sse/reference/123456/  -> Handler1
api/v1/xyz_development/members/reference/789045/  -> Handler2

Please note xyz_development is not fixed. It might change based on environment, so i don't want to hardcode it in URL patterns.

Requests with url pattern with /sse should be handled by Handler1 and any other requests shoud be handled by Handler2.

I'm registering routes like this:

router.route("/sse/*).handler(new Handler1());
router.route("/api/v1/*).handler(new Handler2());

But when requests come with /sse , those are going to Handler2 instead of Handler1 as prefix is same (/api/v1/...)

I tried subRouters and pathRegex() API as well but couldn't make it work. Please suggest a way to acheive this.


Solution

  • After couple of trial and errors, finally found the solution. Defining routes like below worked:

    router.route().pathRegex("/api/v1/.*/sse/.*").handler(new Handler1());
    router.route().path("/api/v1/*").handler(new Handler2());
    

    Now requests with /sse are being handled by Handler1 and other requests are being handled by Handler2.

    I welcome if there are other ways to achieve this.