Java ReSTLet - 如何动态地将资源附加到路由器?

标签 java resources restlet mashup

这是我正在尝试做的事情的基础。我正在从事一个项目,在该项目中我从各种 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/ 并将其绑定(bind)到类 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 对象。

关于Java ReSTLet - 如何动态地将资源附加到路由器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30287619/

相关文章:

java - ReSTLet,带参数的 get 示例

java - 聊天应用程序的 TextGUI.getText() 返回 null

python - 清除pyglet资源缓存

java - ReSTLet 1.1 访问控制 header 问题

java - OSGI中的ReSTLet找不到com/sun/net/httpserver/HttpHandler

c# - 如何从 .NET 程序集中检索二进制文件?

java - Struts 1.x 迁移到什么?

java - 当输入参数大小增加时输出中断

java - 以编程方式在 ExpandableListView 中执行 subview 单击

c++ - 如何检查变量是否有资源?