本文介绍了将CDI拦截器注入到Jersey RS服务(资源)中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个tomcat 8 + jersey 2.5.1 +焊接CDI应用程序,在大多数情况下都可以很好地工作。失败的地方是我无法使用CDI拦截器拦截泽西资源方法调用。这是有道理的,因为球衣资源类不是CDI bean。然后,有什么方法可以使CDI拦截器在球衣上工作?提出此问题的另一种方法:可以将CDI bean用作Jersey资源吗?

So, I have a tomcat 8 + jersey 2.5.1 + weld CDI app that works very well in most cases. Where it fails is that I am unable to intercept jersey resource method calls with a CDI interceptor. This makes sense because a jersey resource class is not a CDI bean. Then, is there any way to make a CDI interceptor work in jersey? Another way to ask this question: Can a CDI bean be used as a Jersey resource?

谢谢!

编辑:
在我编写RESTful之前在使用Jersey的资源中,我有用于开始和提交数据库事务的CDI拦截器。我确实需要遵循相同或相似的模式,以在我的RESTful球衣资源中实现这种跨领域事务注入。这是问这个问题的主要原因。

Before I wrote my RESTful resources using Jersey, I had CDI interceptors that were used to begin and commit database transactions. I really need to follow the same or similar pattern to implement this cross-cutting transaction injection in my RESTful jersey resources. That is the main reason for asking this question.

再次感谢!

推荐答案

是,但是自Jersey的DI以来基于而非CDI,您需要一座桥梁。

Yes, but since Jersey's DI is based on hk2 and not CDI, you need a bridge.

在玻璃鱼中,这样的桥梁由模块 jersey-gf-cdi 实现:

In glassfish such a bridge is realized by the module jersey-gf-cdi:

<dependency>
    <groupId>org.glassfish.jersey.containers.glassfish</groupId>
    <artifactId>jersey-gf-cdi</artifactId>
    <version>2.6</version>
</dependency>

该模块自动注册并可以在Tomcat上正常运行(假设您已正确引导Jersey和Weld) 。

The module registers itself automatically and works on Tomcat beautifully (assuming you have correctly bootstrapped both Jersey and Weld).

不幸的是,2.6之前的版本仅依赖JNDI,在Tomcat不允许的 java:comp / BeanManager下搜索提供程序。

Unfortunately, the versions before 2.6 relies on JNDI only, searching the provider under 'java:comp/BeanManager' which Tomcat does not allow.

此行为的修复程序适用于2.6(我在一段时间前提出的拉取请求),并依赖于 CDI.current()。getBeanManager()

A fix for this behaviour is available for 2.6 (a pull request I made some time ago), and falls back on CDI.current().getBeanManager().

我在Tomcat 7上对其进行了测试,并且可以正常工作,应该也可以在Tomcat 8上使用。

I tested it on Tomcat 7 and works correctly, should work on Tomcat 8 too.

这篇关于将CDI拦截器注入到Jersey RS服务(资源)中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:40
查看更多