Jacob van Lingen Jacob van Lingen - 1 year ago 69
Java Question

Dynamic applicationpath

A new application of ours uses multi-tenancy with multiple database. By providing a tenant id in the URL, we can select the right datasource.

But by using that kind of method, the namespace of the URL becomes dynamic (e.g.: instead of

the url changes to
). So is it possible to use a dynamic @ApplicationPath?

Just as it is possible to use a variable in the @Path annotation, could I write something like

Answer Source

Seems applicationpath does not support dynamic segments. In the end we fixed it by using sub-resources:


public class TenantConfig extends ResourceConfig {

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



public class TenantsController {

   //register all your controllers including path here

    public Class<SomethingController> something() {
        return SomethingController.class;


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

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