abbas
abbas

Reputation: 432

CDI injection with ExceptionMapper- @inject null

I have the following ExceptionMapper:

 @Provider
 public class GenericExceptionMapper 
 implements ExceptionMapper<Exception> {
 @Inject
 private ExceptionDAO exceptionDAO;

@Override
public Response toResponse(Exception e) {
    LOGGER.error(e.getMessage(), e);
    return Response.status(INTERNAL_SERVER_ERROR)
            .type(MediaType.TEXT_PLAIN)
            .entity(e.getMessage())
            .build();
}

The exceptionDAO is always null. I have a beans.xml

 <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://java.sun.com/xml/ns/javaee"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="  http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee /beans_1_0.xsd"
></beans>

I have tried injecting alot of classes, but they always show as null.

Upvotes: 2

Views: 938

Answers (1)

dcsohl
dcsohl

Reputation: 7406

Both the class being managed (ExceptionDAO) and the class injecting it need to be CDI-aware. In this case, this will probably involve marking both classes with @RequestScoped (or other CDI normal scope).

Do not get rid of the @Provider or other JAX-RS annotations; just add the CDI annotation.

Upvotes: 1

Related Questions