1

我正在尝试为我的应用程序添加一种 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。任何帮助表示赞赏

4

2 回答 2

2

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

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

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

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

于 2017-08-08T17:04:13.520 回答
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 {}
     }
 }
于 2017-08-08T12:00:45.340 回答