2

I have an ExceptionMapper defined as following

@Provider
public class MyExceptionMapper implements ExceptionMapper<Throwable> {
   @Inject
   private Manager myManager;

   @Override
   public Response toResponse(Throwable exception) {
      // My implementation
   }
}

Deploying this code on glassfish 4 results with exception:

org.glassfish.hk2.api.UnsatisfiedDependencyException: 
There was no object available for injection at
Injectee(requiredType=Manager,parent=MyExceptionMapper,qualifiers {}),position=-1,optional=false,self=false,unqualified=null,955636053)

When I use @Context instead of @Inject I do not see the exception but myManager is null I tried making MyManager as @ManagedBean, @Singleton or an EJB (Stateless, Singleton) and non works

hmashlah
  • 144
  • 7

3 Answers3

0

In JEE6 (with glassfish 3) you have to add

@javax.annotation.ManagedBean

to the provider implementation. Possibly this works also for Glassfish 4

As far as I know, this issues comes from the following. CDI is not in place to manage the dependencies of restful services and providers by default. But when adding @ManagedBean you enable CDI to create the instance.

Here is an example where I introduced CDI to a restful service using jersey.

Peter
  • 783
  • 4
  • 14
0

You can follow the updates regarding this issue here https://java.net/jira/browse/JERSEY-2393

hmashlah
  • 144
  • 7
0

You could use OmniFaces Beans to get a CDI managed bean instance in your ExceptionMapper:

Beans.getInstance(Bean.class)

I'm using this with @javax.ejb.Stateless beans containing @javax.persistence.PersistenceContext.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102