动态应用程序路径

问题描述:

我们的一个新应用程序使用多租户和多个数据库。通过在URL中提供租户ID,我们可以选择正确的数据源。动态应用程序路径

但是,通过使用这种方法,URL的名称空间变为动态(例如:而不是/api url更改为/{id}/api)。那么是否可以使用动态@ApplicationPath

正如可以在@Path注释中使用变量一样,我可以写一些类似@ApplicationPath("/tenants/{id}/api")的东西吗?

+2

我不认为你可以在'@ ApplicationPath'中拥有路径参数。但是你可以使用路径参数'@Path(“/ {id}/tenants”)来启动'@ Path'' –

+0

@CássioMazzochiMolin:是的,我也开始认为在@ ApplicationPath中使用变量是只是不支持(无论如何,我在互联网上没有发现任何关于这个问题的提及)。 –

似乎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 
    } 
}