2017-08-04 128 views
0

我想为我的应用程序添加一种hello/name(用于健康检查),但我不想做我的chainAction的那部分。Ratpack处理程序 - 如何添加多个前缀

.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)) 

没有运气..

我没关系与添加第二前缀e.g /问候/你好。添加第二个前缀也不起作用。

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

回答

1

顺序在处理程序链超级重要。请求从顶部流向底部,所以您的最不具体的绑定应该在底部。

对于你想做什么,你可以这样做:

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

而且,你不应该/的明确添加到您的路径绑定。绑定在当前链中的位置决定了前面的路径,所以/是不必要的。

+0

谢谢丹。我想我的名字前面有“/”。 –

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

谢谢John。我选择了丹的答案因为我想要的前缀解决方案 –