mercredi 29 juillet 2015

Liberty profile not injecting ejb in servlet

I googled without luck trying to understand why Liberty Profile 8.5.5.6 does not inject EJB into a servlet

I put everything into a war. The war deployed fine, but when I call the servlet I get the following error:

Exception thrown by application class 'org.javaee7.servlet.simple.UserServlet.doGet:23'
java.lang.NullPointerException:
at org.javaee7.servlet.simple.UserServlet.doGet(UserServlet.java:23)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1285)
at [internal classes]

On this link I saw a comment that I should use @ManagedBean

When I try this, the null pointer exception goes away but now I get this error:

Error 404: javax.servlet.UnavailableException: SRVE0319E: For the [UserServlet] servlet, org.javaee7.servlet.simple.UserServlet servlet class was found, but a resource injection failure has occurred. CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/org.javaee7.servlet.simple.UserServlet/service reference. The exception message was: The EJB reference in the lps.war module of the lps application could not be resolved; nested exception is: com.ibm.ejs.container.EJBNotFoundException: EJB with interface org.javaee7.service.UserService not present in application lps. 

Here is my code:

@Stateless
public class UserServiceImpl implements UserService {
    public User getUser(Long id) {
        return new User(id, "Gary");
    }
}

@Remote
public interface UserService {
    public User getUser(Long id);
}

@ManagedBean
public class UserServlet extends HttpServlet {
    @EJB(mappedName = "UserServiceImpl")
    UserService service;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = service.getUser(1L);
        response.getWriter().print(user == null ? "no user" : user.toString());
    }
}

Aucun commentaire:

Enregistrer un commentaire