Java Restlet-如何动态将资源附加到路由器?
作者:互联网
这是我正在尝试做的基础.我正在一个项目中,我正在通过各种Web API创建混搭页面.混搭页面包含国家信息.
我有一个BLL层,在其中创建混搭页面(使用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方法(从TemplateManager中)返回html:
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;
}
在CountryCountsource的定义中,您可以访问查询参数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对象.
标签:restlet,mashup,java,resources 来源: https://codeday.me/bug/20191120/2042969.html