在 Tomcat 7 中将 JAX-RS (RESTEasy) 与 CDI (Weld) 结合使用

发布于 2024-11-15 09:38:22 字数 3418 浏览 2 评论 0原文

在 JSF 2.0 应用程序(在 Tomcat 7 上运行并使用焊接 1.1.1.Final)中,我想建议我的用户下载一些二进制文件(.doc、.pdf 等)。

为了满足该需求,我想使用 JAX-RS (RESTEasy 2.2.0.Final) 资源 bean(用 @Path 注释)。问题是在该 bean 内部,我想从使用 @Inject 注释进行注释的字段调用服务。

实际上,就像焊接用户尝试类似的事情一样,我遇到了NullPointerException :Weld 不会向我提供服务。

所以我读了 JBoss 社区 wiki 上的一篇文章,讨论如何将 RESTEasy 与 CDI 集成,所以我已经使我的 Maven War 项目依赖于 org.jboss.resteasy:resteasy-cdi ,这是我的 web.xml :

<!-- Weld -->
<resource-env-ref>
    <description>Object factory for the CDI Bean Manager</description>
    <resource-env-ref-name>BeanManager</resource-env-ref-name>
    <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>
<listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<filter>
    <filter-name>ConversationPropagationFilter</filter-name>
    <filter-class>org.jboss.weld.servlet.ConversationPropagationFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ConversationPropagationFilter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

<!-- Resteasy -->
<listener>
    <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<context-param>
    <param-name>resteasy.injector.factory</param-name>
    <param-value>org.jboss.resteasy.cdi.CdiInjectorFactory</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>
<servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
</context-param>

现在的问题是当我的应用程序引导时,我遇到了这个异常:

java.lang.RuntimeException: Unable to instantiate InjectorFactory implementation.
    at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:141)
    at org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap.contextInitialized(ResteasyBootstrap.java:28)
...
Caused by: java.lang.NullPointerException
    at org.jboss.resteasy.cdi.CdiInjectorFactory.lookupBeanManager(CdiInjectorFactory.java:116)
    at org.jboss.resteasy.cdi.CdiInjectorFactory.<init>(CdiInjectorFactory.java:41)
...
    at java.lang.Class.newInstance(Class.java:308)
    at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:133)

当我删除resteasy.injector.factory上下文参数时,我在尝试获取时遇到了NPE我的服务来自用 @Inject 注释的字段变量...

有谁知道如何配置 RESTEasy beans 以由 Weld 管理(并可以注入 JAX-RS 资源)?

In a JSF 2.0 application (running on Tomcat 7 and using weld 1.1.1.Final), I want to propose my user to download some binary files (.doc, .pdf etc).

In order to fulfil that need, I want to use a JAX-RS (RESTEasy 2.2.0.Final) resource bean (annotated with @Path). The problem is that inside that bean, I want to call a service from a field annotated with @Inject annotation.

Actually, like a weld user trying a similar thing I've got a NullPointerException : Weld doesn't inject me my service.

So I read a post on JBoss community wiki talking about how to integrate RESTEasy with CDI so I've made my maven war project depend on org.jboss.resteasy:resteasy-cdi and here is my web.xml :

<!-- Weld -->
<resource-env-ref>
    <description>Object factory for the CDI Bean Manager</description>
    <resource-env-ref-name>BeanManager</resource-env-ref-name>
    <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>
<listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<filter>
    <filter-name>ConversationPropagationFilter</filter-name>
    <filter-class>org.jboss.weld.servlet.ConversationPropagationFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ConversationPropagationFilter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

<!-- Resteasy -->
<listener>
    <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<context-param>
    <param-name>resteasy.injector.factory</param-name>
    <param-value>org.jboss.resteasy.cdi.CdiInjectorFactory</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>
<servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
</context-param>

The problem now is that when my application bootstraps, I've got that exception :

java.lang.RuntimeException: Unable to instantiate InjectorFactory implementation.
    at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:141)
    at org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap.contextInitialized(ResteasyBootstrap.java:28)
...
Caused by: java.lang.NullPointerException
    at org.jboss.resteasy.cdi.CdiInjectorFactory.lookupBeanManager(CdiInjectorFactory.java:116)
    at org.jboss.resteasy.cdi.CdiInjectorFactory.<init>(CdiInjectorFactory.java:41)
...
    at java.lang.Class.newInstance(Class.java:308)
    at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:133)

And when I remove the resteasy.injector.factory context-param, I've got an NPE when trying to get my service from the field variable annotated with @Inject ...

Does anybody knows how to configure RESTEasy beans to be managed by Weld (and make possible injection in JAX-RS resources) ?

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

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

发布评论

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

评论(2

七颜 2024-11-22 09:38:22

这已在 RESTEasy 主干中修复。有关详细信息,请参阅 https://issues.jboss.org/browse/RESTEASY-558

This has been fixed in RESTEasy trunk. See https://issues.jboss.org/browse/RESTEASY-558 for details.

情话难免假 2024-11-22 09:38:22

如果您有机会并且付出的努力是合理的:尝试使用 JBoss AS 6 进行相同的场景。如果可行,您可能会解决配置问题。如果它因类似(或相同)的异常而失败,您就知道应该归咎于 Weld;-)

两种结果至少应该为您提供下一步的方向...

If you have the chance and the effort is reasonable: Try the same scenario wiht JBoss AS 6. If that works, you'll probably nailed down a configuration issues. If it fails with a similiar (or equal) exception, you know that it's Weld to blame ;-)

Both outcomes should at least give you a direction where to look next...

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