Search code examples
ratpack

Ratpack handlers -how to add more than one prefixes


I am trying to add kind of hello/name (for health check) for my app but I do not want to do that part of my chainAction.

.handlers(chain -> chain
                .prefix("api", TaskChainAction.class))
        );

What it takes to add second hello greeting without using "api" prefix?

I tried

.handlers(chain-> chain
                    .get("/:name", ctx -> ctx.render("Hello"+ctx.getPathTokens().get("name"))))
                .handlers(chain -> chain
                    .prefix("task", TaskChainAction.class))
            );

and

.handlers(chain-> chain
                    .get("/:name", ctx -> ctx.render("Hello"+ctx.getPathTokens().get("name"))))
                .handlers(chain -> chain
                .prefix("task", TaskChainAction.class))

No luck..

I am okay with adding second prefix e.g /greetings/hello. Adding second prefix is not working either.

I am using 1.4.6 version of ratpack. Any help is appreciated


Solution

  • Order is super important in the handler chain. The request flows from the top to the bottom, so your least-specific bindings should be at the bottom.

    For what you're trying to do, you can do something like:

    RatpackServer.start(spec -> spec
      .handlers(chain -> chain
        .prefix("api", ApiTaskChain.class)
        .path(":name", ctx -> 
          ctx.render("Hello World")
        )
      )
    );
    

    Also, you should not add /'s explicitly to your path bindings. A binding's position in its current chain dictates the preceding path, so / is unnecessary.