2016-09-16 32 views
2

我們的一個新應用程序使用多租戶和多個數據庫。通過在URL中提供租戶ID,我們可以選擇正確的數據源。動態應用程序路徑

但是,通過使用這種方法,URL的名稱空間變爲動態(例如:而不是/api url更改爲/{id}/api)。那麼是否可以使用動態@ApplicationPath

正如可以在@Path註釋中使用變量一樣,我可以寫一些類似@ApplicationPath("/tenants/{id}/api")的東西嗎?

+2

我不認爲你可以在'@ ApplicationPath'中擁有路徑參數。但是你可以使用路徑參數'@Path(「/ {id}/tenants」)來啓動'@ Path'' –

+0

@CássioMazzochiMolin:是的,我也開始認爲在@ ApplicationPath中使用變量是隻是不支持(無論如何,我在互聯網上沒有發現任何關於這個問題的提及)。 –

回答

0

似乎applicationpath不支持動態段。最後,我們固定它通過使用sub-resources

配置

@ApplicationPath("tenants") 
public class TenantConfig extends ResourceConfig { 

    public TenantConfig(ObjectMapper mapper) { 
     //set provider + add mapper 

     register(TenantsController.class); 
    } 
} 

TenantsController

@Path("/{id}/api") 
public class TenantsController { 

    //register all your controllers including path here 

    @Path("/somethings") 
    public Class<SomethingController> something() { 
     return SomethingController.class; 
    } 
} 

SomethingController

@Component 
//Don't use @Path, as path info is already defined in the TenantsController 
public class SomethingController { 
    //do your stuff here; 

    @GET 
    @Path("/{id}") //Path for this example would be /tenants/{id}/api/somethings/{id} 
    public JsonApiResult get(@PathParam("id") int id) { 
     //retrieve one something 
    } 
}