JAX-RS 中的横切关注点
我正在 JAX-RS (JSR-311) 中寻找一种机制,以允许我提炼出一些特定于我的应用程序的横切关注点。例如,我的应用程序具有针对每个请求查找或构建的特定上下文。我希望能够在集中位置执行这种类型的逻辑,然后以某种方式附加到上下文,以供各种资源用于请求的其余部分。如果我可以仅对 URL 的某些子集执行这些类型的操作,那就更好了。
JAX-RS 为路径段、cookie、标头等提供的默认注入很棒,但是这些部分的自定义解释又如何呢?我真的不想每次需要时都必须构建它。我宁愿有一种方法来指定它的构建方式,然后将上下文组件作为我的资源方法的一部分注入。
存在这样的钩子吗?我可以操纵提供者模型来做到这一点吗?顺便说一句,我想尽可能长时间地保持实现独立(Jersey、RESTEasy 等)。
预先感谢您的任何见解。
I'm looking for a mechanism within JAX-RS (JSR-311) to allow me to distill out some of my cross-cutting concerns specific to my app. For example, my app has certain context which is looked up or built for each request. I'd like to be able to have this type of logic be performed in a centralized location and then somehow be attached to the context to be utilized by various resources for the remainder of the request. It would be even better if I could perform these types of actions for only some subsets of URLs.
The default injection that JAX-RS provides for path segments, cookie, header, etc. is great but what about custom interpretation of those parts? I really don't want to have to build that each time I need it. I'd rather have a way to specify how it is built and then just have the context component injected as part of my resource method.
Do any such hooks exist? Can I manipulate the providers model to do this? BTW, I want to stay implementation independent (Jersey, RESTEasy, etc.) as long as possible.
Thanks in advance for any insight.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 ContextResolver 提供程序向资源或其他提供程序提供任何上下文。基本上,您需要为要注入的任何上下文实现 javax.ws.rs.ext.ContextResolver。不要忘记用
@Provider
注释它并注册。You can use
ContextResolver
provider to provide any context to the resource or to another provider. Basically you need to implementjavax.ws.rs.ext.ContextResolver<T>
for any context you want to get injected. Don't forget to annotate it with@Provider
and register.在某种奇怪的扭曲中,标准以一种奇妙的可组合方式协同工作(JAX-RS 和 CDI),IBM 编写了一个教程,可以涵盖我的具体问题。 关于结合 CDI 和 JAX-RS 的更大教程,本文专门讨论使用 CDI(Java 上下文和依赖注入)装饰器和方法拦截器来实现 JAX-RS 资源中的横切关注点:
更新:我刚刚能够在GlassFish 3.1中使用它。关键(我发现的所有示例都没有显示)是您必须确保 CDI 管理资源实例的生命周期(以便它可以用拦截器包装)。实例化自己,然后在 Application.getSingletons() 方法中返回不起作用。
我要回去看看是否也能把它带到码头。
更新 2: Jetty(以及任何其他非 J2EE servlet 容器,例如 Tomcat)使用 CDI 进行设置有点痛苦。我认为 GlassFish 的集成要容易得多。以下博客概述了 Jetty 所需的一些步骤:
In some sort of bizaro twist, standards are working together (JAX-RS & CDI) in a wonderfully composable manner, and IBM wrote a tutorial which which may cover my specific question. Part of a larger tutorial on combining CDI and JAX-RS, this article specifically addresses using CDI (Java Contexts and Dependency Injection) decorators and method interceptors to implement cross-cutting concerns in JAX-RS resources:
Update: I was just able to get this to work in GlassFish 3.1. The key (which none of the examples I've found showed) is that you have to make sure CDI manages the lifespan of your resource instances (so it can wrap with the interceptors). Instantiating yourself and then returning in the Application.getSingletons() method does not work.
I'm going to go back and see if I can get it to go in Jetty too.
Update 2: Jetty (and by extension probably any other non-J2EE servlet containers like Tomcat) are kind of a pain to get set up with CDI. I think GlassFish is a much easier integration. Here is one blog that outlines some of the steps needed for Jetty: