RESTEasy - 动态添加资源类
通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我自己没有尝试过,但我找到了一篇博客文章,其中描述了这一点:
http://sarbarian.wordpress.com/2010/ 03/07/resteasy-and-osgi-perfect-match/
在部署期间,RESTEasy 将其注册表放入 servlet 上下文中。博客中建议测试的想法是,从 servlet 上下文中获取注册表,然后添加资源类。
像这样的东西:
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:
我自己也尝试过,虽然很伤脑筋,但效果很好。我们有一个基础设施,其中几个独立的项目(插件)由我们的主应用程序导入和加载,我们希望在现有的 RestEasy API(和 Swagger 文档)中包含对它们的访问。
我们最初将类加载硬编码到我们的主 Rest Application 类中,这要求所有插件始终显示在我们的 API 中。为了避免这种情况并恢复这些项目的独立性(在任何给定时间都可能包含所有、部分或不包含),我们创建了返回功能/目标类本身的动态方法,该方法在项目的引导阶段加载RestEasy 框架。它就像一个魅力。
在我们的 Rest Application 类中,我们实现如下:
这是旧的方法。新方法是循环遍历我们所有的插件并返回插件提供的类的 hashSet:
pluginsHelper 具有各种共享插件方法和管理工具,例如 getClasses(),它基本上执行以下操作:
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:
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:
The pluginsHelper has sundry shared plugin methods and administrative tools, like getClasses(), which does basically this:
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.
我在嵌入式环境中使用 Reateasy 和 Undertow,手动实例化
ResteasyDeployment
,并注册 Resource 类非常简单,请参见下面的代码片段:I use Reateasy with Undertow in embedded environment, where I manually instantiate
ResteasyDeployment
, and register Resource class is really easy, see code snippet blow: