views:

207

answers:

1

I'm trying to inject Stateless EJB into my JAX-RS webservice via Annotations. Unfortunately the EJB is just null and I get a NullPointerException when I try to use it.

@Path("book")
public class BookResource {

    @EJB
    private BookEJB bookEJB;

    public BookResource() {
    }

    @GET
    @Produces("application/xml")
    @Path("/{bookId}")
    public Book getBookById(@PathParam("bookId") Integer id)
    {
        return bookEJB.findById(id);
    }
}

What am I wrong doing?

Here are some informations about my machine:

  • Glassfish 3.1
  • Netbeans 6.9 RC 2
  • Java EE 6

Can you guys put some working example?

+5  A: 

I am not sure this is supposed to work. So either:

Option 1: Use the injection provider SPI

Implement a provider that will do the lookup and inject the EJB. See:

Option 2: Make the BookResource an EJB

@Stateless
@Path("book")
public class BookResource {

    @EJB
    private BookEJB bookEJB;

    //...
}

See:

Option 3: Use CDI

@Path("book")
@RequestScoped
public class BookResource {

    @Inject
    private BookEJB bookEJB;

    //...
}

See:

Pascal Thivent
It's working great. When i add @Stateless annotation to my JAX-RS. Thank you. You're really cool guy.
Zeck
@Zeck: You're welcome. Feel free to upvote (and maybe even accept) this answer then :)
Pascal Thivent