RESTEasy - 动态添加资源类

发布于 2024-11-28 17:25:49 字数 90 浏览 2 评论 0原文

通过 RESTEasy,我实现了 Application 的子类来提供单例资源列表。有没有办法稍后动态添加另一个单例?我还没有从 API 文档中找到实现这一点的方法。

With RESTEasy I've implemented a subclass of Application to provide a list of singleton resources. Is there a way to add another singleton dynamically later on? I've not found a way to do it from the API docs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈场末日恋爱 2024-12-05 17:25:49

我自己没有尝试过,但我找到了一篇博客文章,其中描述了这一点:
http://sarbarian.wordpress.com/2010/ 03/07/resteasy-and-osgi-perfect-match/

在部署期间,RESTEasy 将其注册表放入 servlet 上下文中。博客中建议测试的想法是,从 servlet 上下文中获取注册表,然后添加资源类。

像这样的东西:

import org.jboss.resteasy.spi.Registry;

Object resource = new MyService();
Registry registry = (Registry) context.getAttribute(Registry.class.getName());
registry.addSingletonResource(resource);

I have not tried this myself, but I found a blog post where this is described:
http://sarbarian.wordpress.com/2010/03/07/resteasy-and-osgi-perfect-match/

During deployment, RESTEasy puts it's registry in the servlet context. The idea suggstested in the blog, is that you fetch the registry from the servlet context, and then add your resource class.

Something like this:

import org.jboss.resteasy.spi.Registry;

Object resource = new MyService();
Registry registry = (Registry) context.getAttribute(Registry.class.getName());
registry.addSingletonResource(resource);
秋叶绚丽 2024-12-05 17:25:49

我自己也尝试过,虽然很伤脑筋,但效果很好。我们有一个基础设施,其中几个独立的项目(插件)由我们的主应用程序导入和加载,我们希望在现有的 RestEasy API(和 Swagger 文档)中包含对它们的访问。

我们最初将类加载硬编码到我们的主 Rest Application 类中,这要求所有插件始终显示在我们的 API 中。为了避免这种情况并恢复这些项目的独立性(在任何给定时间都可能包含所有、部分或不包含),我们创建了返回功能/目标类本身的动态方法,该方法在项目的引导阶段加载RestEasy 框架。它就像一个魅力。

在我们的 Rest Application 类中,我们实现如下:

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<>();
    classes.add(mainAPIStuff.class);
    classes.add(plugin1.class);
    classes.add(plugin2.class);
    classes.add(plugin3.class);
    .
    .
    .
    return classes;
}

这是旧的方法。新方法是循环遍历我们所有的插件并返回插件提供的类的 hashSet:

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<>();
    classes.add(mainAPIStuff.class);
    classes.addAll(pluginsHelper.getClasses());
    return classes;
}

pluginsHelper 具有各种共享插件方法和管理工具,例如 getClasses(),它基本上执行以下操作:

public static Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<>();

    // Probe plugins for their Rest Class implementation
    List plugins = getCurrentlyInstalledPluginsFromOurSystem();
    for (PluginObject plugin : plugins) {
        Class<?> clazz = plugin.getPluginRestClass();
        if (clazz != null) {
            classes.add(clazz);
        }
    }

    return classes;
}

RestEasy 动态构建 Rest 目标当任何目标被调用时,所有插件的安装、启动、配置都已经完成,并且系统在调用之前处于正确的状态。理解这一点非常有价值,因为这样您就可以在调用时执行所有 RestEasy 类操作和加载。

I have tried this myself, and although it was nerve-wracking, it works great. We've got an infrastructure where several independent projects (plug-ins) are imported and loaded by our main application, and we wanted to include access to them in our existing RestEasy API (and Swagger documentation).

We originally hard-coded the class-loading into our main Rest Application class, which required all of the plug-ins to be displayed at all times in our API. To avoid that and to re-instate the independence of those projects (all, some, or none may be included at any given time), we created dynamic methods that return the feature/target class itself, which is loaded during the bootstrap phase of the RestEasy framework. It worked like a charm.

Within our Rest Application class, we implemented it as follows:

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<>();
    classes.add(mainAPIStuff.class);
    classes.add(plugin1.class);
    classes.add(plugin2.class);
    classes.add(plugin3.class);
    .
    .
    .
    return classes;
}

That was the old way. The new way was to loop through all of our plugins and return a hashSet of the classes that the plugins provide:

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<>();
    classes.add(mainAPIStuff.class);
    classes.addAll(pluginsHelper.getClasses());
    return classes;
}

The pluginsHelper has sundry shared plugin methods and administrative tools, like getClasses(), which does basically this:

public static Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<>();

    // Probe plugins for their Rest Class implementation
    List plugins = getCurrentlyInstalledPluginsFromOurSystem();
    for (PluginObject plugin : plugins) {
        Class<?> clazz = plugin.getPluginRestClass();
        if (clazz != null) {
            classes.add(clazz);
        }
    }

    return classes;
}

RestEasy builds the Rest targets dynamically when any target is called, so all of the plugin installation, startup, config has already completed and the system is in the correct state before it is called. This is very valuable to understand, since then you can do all of the RestEasy class manipulation and loading at call time.

長街聽風 2024-12-05 17:25:49

我在嵌入式环境中使用 Reateasy 和 Undertow,手动实例化 ResteasyDeployment,并注册 Resource 类非常简单,请参见下面的代码片段:

    UndertowJaxrsServer undertowJaxrsServer = new UndertowJaxrsServer();
    ResteasyDeployment resteasyDeployment = new ResteasyDeployment();

    undertowJaxrsServer.start();
    resteasyDeployment.start();

    final DeploymentInfo undertowDeployment =
        undertowJaxrsServer
            .undertowDeployment(resteasyDeployment)
            .setContextPath("/abc")
            .setDeploymentName("TEST")
            .setClassLoader(Thread.currentThread().getContextClassLoader());

    undertowJaxrsServer.deploy(undertowDeployment);

    resteasyDeployment.getRegistry().addSingletonResource(new ResourceApiImpl());

I use Reateasy with Undertow in embedded environment, where I manually instantiate ResteasyDeployment, and register Resource class is really easy, see code snippet blow:

    UndertowJaxrsServer undertowJaxrsServer = new UndertowJaxrsServer();
    ResteasyDeployment resteasyDeployment = new ResteasyDeployment();

    undertowJaxrsServer.start();
    resteasyDeployment.start();

    final DeploymentInfo undertowDeployment =
        undertowJaxrsServer
            .undertowDeployment(resteasyDeployment)
            .setContextPath("/abc")
            .setDeploymentName("TEST")
            .setClassLoader(Thread.currentThread().getContextClassLoader());

    undertowJaxrsServer.deploy(undertowDeployment);

    resteasyDeployment.getRegistry().addSingletonResource(new ResourceApiImpl());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文