Ratpack处理程序 - 如何添加多个前缀

时间:2017-08-04 15:10:36

标签: ratpack

我正在尝试为我的应用添加一种hello / name(用于健康检查),但我不想做我的chainAction的那部分。

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

如果不使用" api"添加第二个问候语,需要什么?前缀?

我试过

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

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

没有运气..

我可以添加第二个前缀,例如/ greetings / hello。添加第二个前缀也不起作用。

我正在使用1.4.6版本的ratpack。任何帮助表示赞赏

2 个答案:

答案 0 :(得分:2)

订单在处理程序链中非常重要。请求从顶部流向底部,因此最不具体的绑定应位于底部。

对于您尝试做的事情,您可以执行以下操作:

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

此外,您不应明确将/添加到路径绑定中。当前链中的绑定位置决定了前面的路径,因此/是不必要的。

答案 1 :(得分:1)

以下是我在ratpack.groovy文件中使用的具有多个路径的处理程序链:

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 {}
     }
 }