我们的新应用程序使用多租户和多个数据库。通过在URL中提供租户ID,我们可以选择正确的数据源。
但是通过使用这种方法,URL的名称空间变为动态的(例如:而不是/api
网址更改为/{id}/api
)。那么是否可以使用动态@ApplicationPath?
正如可以在@Path注释中使用变量一样,我可以编写类似@ApplicationPath("/tenants/{id}/api")
的内容吗?
答案 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}}