Java Restlet-如何动态将资源附加到路由器?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java Restlet-如何动态将资源附加到路由器?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3302字,纯文字阅读大概需要5分钟。
内容图文
![Java Restlet-如何动态将资源附加到路由器?](/upload/InfoBanner/zyjiaocheng/662/ba9c2f9484644745af3b456ca66bd5ea.jpg)
这是我正在尝试做的基础.我正在一个项目中,我正在通过各种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对象.
内容总结
以上是互联网集市为您收集整理的Java Restlet-如何动态将资源附加到路由器?全部内容,希望文章能够帮你解决Java Restlet-如何动态将资源附加到路由器?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。