GlassFish 4 + JAX-RS Filter with @EJB

僤鯓⒐⒋嵵緔 提交于 2019-12-10 14:44:01

问题


I'm developing an REST application using Glassfish 4.0.

In resource classes I can get injection to work by making the class @Stateless and injecting via @EJB (injected class is an stateless EJB).

However this approach does not work in an JAX-RS filter. I cannot get injection to work at all.

See code below:

@Provider
public class UpdateFilter implements ContainerRequestFilter {

    @EJB
    private MyBeanInterface doStuffBean;

    @Override
    public void filter(ContainerRequestContext requestContext) {

        ...
    }
}

doStuffBean is always null.

Any suggestions?


回答1:


I believe the @EJB only works in Java EE managed classes like other EJBs and Servlets.

If you are using CDI you could use @Inject annotation instead but if this class is not a ManagedBean then you will need to do a lookup.




回答2:


Try to use CDI by replacing @Stateless by @ManagedBean and @EJB by @Inject. This works for me in JAX-RS.

If you need EJB for other things than injection it may work for you to keep the double annotation @Stateless @ManagedBean.



来源:https://stackoverflow.com/questions/17951001/glassfish-4-jax-rs-filter-with-ejb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!