2013-07-15 26 views
1

我使用sitebricks在Google App Engine上構建了一個RESTful API。我爲我的GuiceCreator中的所有/ rest/*網址註冊了兩個過濾器。 如何使用filter("/rest/*)語法但排除一個特定的URL?我希望/ rest/*下的所有內容都會被過濾,除了/ rest/1/foo。如何在ServletModule.configureServlets中註冊過濾器時排除URL?

我可以枚舉實際需要過濾的所有URL。但是這樣做的一個明顯的缺點是,如果我決定添加或刪除端點,將難以維護。

new ServletModule() { 
    @Override 
    protected void configureServlets() { 
     filter("/rest/*").through(ObjectifyFilter.class); 
     filter("/rest/*").through(SomeOtherFilter.class); 
    } 
} 

我要尋找一個結構類似

filter("/rest/*").exclude("/rest/1/foo").through(ObjectifyFilter.class). 

回答

0

Thanks to dhanji,我用filterRegex(),而不是filter()固定這一點。在我的正則表達式中,我使用的是negative lookbehind assertion。這將過濾所有/rest/.*網址,但以/[0-9]/foo結尾的網址除外。

new ServletModule() { 
    @Override 
    protected void configureServlets() { 
    filter("^/rest/.*(?<!/\\d/foo)$").through(ObjectifyFilter.class); 
    filter("^/rest/.*(?<!/\\d/foo)$").through(SomeOtherFilter.class); 
    } 
} 
相關問題