【问题标题】:Java Restlet - How do I dynamically attach resources to the router?Java Restlet - 如何将资源动态附加到路由器?
【发布时间】:2015-05-17 14:01:59
【问题描述】:

这是我正在尝试做的基本操作。我正在从事一个项目,我正在从各种 Web API 创建一个混搭页面。混搭页面包含国家/地区信息。

我有一个 BLL 层,我在其中创建混搭页面(使用块库输入国家/地区的 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 的代码完全符合要求。发布的代码已更改为他的版本。

【问题讨论】:

  • @TimBiegeleisen 你能指点一下吗? :)
  • 如果您对我的回答有任何疑问,请告诉我。如果您愿意,我们可以进行聊天。
  • 这正是我所需要的,谢谢!我想我将其标记为已解决。

标签: java resources restlet mashup


【解决方案1】:

您应该使用查询参数,而不是尝试对 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 对象。如果当前不存在这样的方法,那么您将需要找到一种方法将传入的查询参数映射到代码中的实际 Country 对象。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-10-08
  • 1970-01-01
  • 2016-08-16
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
相关资源
最近更新 更多