Java 动态应用程序路径
我们的一个新应用程序使用多数据库的多租户。通过在URL中提供租户id,我们可以选择正确的数据源 但是通过使用这种方法,URL的名称空间变得动态(例如:URL变为Java 动态应用程序路径,java,jax-rs,Java,Jax Rs,我们的一个新应用程序使用多数据库的多租户。通过在URL中提供租户id,我们可以选择正确的数据源 但是通过使用这种方法,URL的名称空间变得动态(例如:URL变为/{id}/api,而不是/{id}/api)。那么,有可能使用一个动态模型吗 正如可以在注释中使用变量一样,我是否可以编写类似于@ApplicationPath(“/tenants/{id}/api”)?ApplicationPath似乎不支持动态段。最后,我们使用以下方法修复了它: 配置 @ApplicationPath("tenan
/{id}/api
,而不是/{id}/api
)。那么,有可能使用一个动态模型吗
正如可以在注释中使用变量一样,我是否可以编写类似于
@ApplicationPath(“/tenants/{id}/api”)
?ApplicationPath似乎不支持动态段。最后,我们使用以下方法修复了它:
配置
@ApplicationPath("tenants")
public class TenantConfig extends ResourceConfig {
public TenantConfig(ObjectMapper mapper) {
//set provider + add mapper
register(TenantsController.class);
}
}
租户控制器
@Path("/{id}/api")
public class TenantsController {
//register all your controllers including path here
@Path("/somethings")
public Class<SomethingController> something() {
return SomethingController.class;
}
}
@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
}
}
我认为在
@ApplicationPath
中不能有路径参数。但是你可以用路径参数启动@Path
:@Path(“/{id}/tenants”)
@CássioMazzochiMolin:是的,我也开始认为在@ApplicationPath
中使用变量是不受支持的(我在互联网上没有发现任何关于这个问题的提及)。