2

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

Vijay Ram
  • 285
  • 2
  • 15

2 Answers2

2

Here is what I use in my ratpack.groovy file for the handler chain with multiple paths:

handlers{
  path("path_here"){
    byMethod{
      get{
        //you can pass the context to a handler like so:
        context.insert(context.get(you_get_handler)}
      post{
        //or you can handle inline
       }
    }
 }
 path("another_path_here") {
        byMethod {
            get {}
            put {}
     }
 }
jmoney
  • 443
  • 2
  • 10
2

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.

Daniel Woods
  • 1,029
  • 7
  • 10