动态应用路径

时间:2016-09-16 13:49:13

标签: java jax-rs

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

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

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

1 个答案:

答案 0 :(得分:0)

似乎applicationpath不支持动态细分。最后,我们使用sub-resources

修复了它

<强>配置

@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
    }
}

<强> TenantsController

 new Thread(new Runnable() {
        public void run() {
            URL url = new URL("http://echo.jsontest.com/key/value/one/two");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            InputStream is = urlConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            int data = isr.read();
            StringBuilder response = new StringBuilder("");
            if(data!=-1){
                 response.append((char)data);
                 data = isr.read();
             }
        // code below need put back to main thread
            runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        allNotifications.setText("Hi:"+response.toString());
                    }
                });

                }
            });
        }
    }).start();

<强> SomethingController

{{1}}
相关问题