Search code examples
jakarta-eeglassfishjax-rswarear

JAX-RS with EJB not working in EAR while good in WAR


I have a java web project containing JAX-RS resource classes like this:

@Stateless
@Path("user")
public class UserResource {
    @EJB
    BooksResource booksResource;

    ...

    @GET
    public String get() {
        return "User Resource";
    }

    @Path("books")
    public BooksResource getBooksResource() {
        System.out.println("booksResource is " + booksResource);
        return booksResource;
    }

    ...

}

@Stateless
public class BooksResource {

    @GET
    public String get() {
        return "Books Resource";
    }
}

Evething works fine when I run the WAR project.

But when I put it in an EAR project, I can only access /user. And /user/books returns 404 and console prints "booksResource is null".

Seems that EJB is not injected when run in EAR project, but why does it work in WAR project?

I am using Eclipse(indigo JavaEE) and Glassfish3.1.2.


Solution

  • After trying a lot, I found that with exactly the same project(the EAR), sometimes it runs good(both /user and /user/books ok), sometimes it runs wrong(the situation described in the question), especially when the[Build Automatically] button was checked. I think there's something wrong with eclipse WTP or Glassfish.