[英]Java Restlet - How do I dynamically attach resources to the router?
这是我正在尝试做的基础知识。 我正在开发一个项目,我正在从各种Web API创建一个mashup页面。 mashup页面包含国家/地区信息。
我有一个BLL层,我在其中创建mashup页面(使用Chunk Library的国家/地区输入的html页面)。 这是用于设置模板的方法
public String GetTemplate(Country country) throws IOException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("BLL/themes/page.html");
Theme theme = new Theme("src/BLL", "themes");
Chunk html = theme.makeChunk("page", "html");
html.set("countryName", country.getName());
html.set("countryCode", country.getCode());
html.set("countryContinent", country.getContinent());
html.set("countryCapital", country.getCapital());
html.set("countryAreaInSqKm", country.getAreaInSqKm());
html.set("countryPopulation", country.getPopulation());
html.set("countryCurrencyCode", country.getCurrencyCode());
html.set("countryExchangeRate", country.getExchangeRate());
html.set("countryFlagUrl", country.getFlagUrl());
html.set("countryDescription", country.getDescription());
html.set("countryMap", country.getMapLocation());
return html.toString();
}
最终,我将为所有可用国家/地区使用数据库。
那么我想在Restlet服务器上做的是迭代所有国家,如此,或以类似的方式?
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/countries/", Countries.class);
for (Country country : cm.GetAllCountries()) {
router.attach("/countries/" + country.getName(), new CountryTemplate(country));
// Obviously this doesn't work, and it doesn't work with getClass() either.
}
return router;
}
因此我试图让我的资源返回get方法中的html(来自TemplateManager):
public class CountryTemplate extends ServerResource {
CountryManager cm = null;
TemplateManager tm = null;
Country country = null;
public CountryTemplate(Country _country) throws Exception {
cm = new CountryManager();
tm = new TemplateManager();
country = _country;
}
@Get("html")
public String getTemplate() throws IOException, RemoteException, UnsupportedCurrencyException {
return tm.GetTemplate(country);
}
}
确实是这样做的方法,还是我接近这一切都错了?
如果其他人发现自己在这里,Tim的代码完全符合要求。 发布的代码已更改为他的版本。
您应该使用查询参数,而不是尝试对REST资源的动态动态行为进行硬编码。
首先,您可以创建一个新的REST资源/countries/html/
并将其绑定到CountryResource
类。 此资源将接受与您要访问其HTML的国家/地区对应的查询参数countryID
:
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/countries/", Countries.class);
router.attach("/countries/html/", CountryResource.class);
return router;
}
在CountryResource
的定义中,您可以访问查询参数countryID
以获取适当的HTML内容:
public class CountryResource extends ServerResource {
CountryManager cm;
TemplateManager tm;
public CountryTemplate() throws Exception {
// Note: I usually do NOT define a constructor using Restlets,
// but you should be OK doing this
cm = new CountryManager();
tm = new TemplateManager();
}
@Get("html")
public String getTemplate() throws IOException, RemoteException, UnsupportedCurrencyException {
String countryID = getQueryValue("countryID");
Country country = tm.getCountry(countryID);
return tm.GetTemplate(country);
}
}
我假设CountryManager
有一个方法getCountry()
,它可以根据输入String返回一个Country
对象。 如果当前不存在这样的方法,那么您将需要找到一种方法将传入的查询参数映射到代码中的实际Country
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.