Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GlassFish 4 + JAX-RS Filter with @EJB

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?

like image 462
bafitor Avatar asked Nov 13 '22 00:11

bafitor


1 Answers

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.

like image 147
ZeusSelerim Avatar answered Dec 26 '22 06:12

ZeusSelerim