2017-08-04 44 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。我選擇了丹的答案因爲我想要的前綴解決方案 –

相關問題