Java Restlet-如何将资源动态附加到路由器?
以下是我尝试做的基本内容。我正在从事一个项目,在这个项目中,我将从各种web API创建一个mashup页面。mashup页面包含国家信息 我有一个BLL层,在那里我创建了mashup页面(使用区块库输入国家的html页面)。这是用于设置模板的方法Java Restlet-如何将资源动态附加到路由器?,java,resources,restlet,mashup,Java,Resources,Restlet,Mashup,以下是我尝试做的基本内容。我正在从事一个项目,在这个项目中,我将从各种web API创建一个mashup页面。mashup页面包含国家信息 我有一个BLL层,在那里我创建了mashup页面(使用区块库输入国家的html页面)。这是用于设置模板的方法 public String GetTemplate(Country country) throws IOException { ClassLoader classLoader = Thread.currentThread().getConte
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):
真的有这样的方法吗?还是我的做法完全错了
如果其他人发现自己在这里,Tim的代码完全按照要求工作。发布的代码已更改为他的版本。您应该使用查询参数,而不是尝试硬编码REST资源的动态行为 首先,您可以创建一个新的REST资源
/countries/html/
,并将其绑定到类CountryResource
。此资源将接受一个查询参数countryID
,该参数对应于要访问其HTML的国家:
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()
,它可以基于输入字符串返回Country
对象。如果这种方法目前不存在,那么您需要找到一种方法,将传入的查询参数映射到代码中实际的国家
对象。@TimBiegeleisen您能指点一下吗?:)如果你对我的答案有任何疑问,请告诉我。如果你愿意,我们可以聊天。这正是我需要的,谢谢!我想我把它标记为已解决。所以,我会访问这样一个国家:啊,我被纠正了,访问它的正确方式是:
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);
}
}