Liberty profile does not inject ejb into servlet

I googled with no luck trying to figure out why Liberty profile 8.5.5.6 is not injecting EJBs into the servlet

I turned everything into a war. 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]

      

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

When I try to do this, the null pointer exception disappears, but now I am getting 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());
    }
}

      

+3


source to share


1 answer


First - for sure you shouldn't use @ManagedBean

. Servlet is not bean driven. You don't need injection to work. You must have either @WebServlet

in your servlet or configure it via web.xml

.
Second, you should uninstall mappedName

as Liberty doesn't use this.

Please add server.xml

to your question as you may have some functionality.
You must have a function ejbRemote-3.2

or javaee-7.0

.

For remote interfaces, you need to use the following binding:

@EJB(lookup="java:global/AppName/ModuleName/UserServiceImpl!org.javaee7.service.UserService")
private UserService service;

      

You can also use other bindings as shown below, depending on your configuration:

java:global/ExampleApp/ExampleModule/ExampleBean!com.ibm.example.ExampleRemoteInterface    
java:app/ExampleModule/ExampleBean!com.ibm.example.ExampleRemoteInterface    
java:module/ExampleBean!com.ibm.example.ExampleRemoteInterface

      



When your bean is required, you should see the following log message:

[INFO    ] CNTR0167I: The server is binding the com.ibm.example.ExampleRemoteInterface interface of the ExampleRemote enterprise bean in the Example.war module of the Example application.  
The binding location is: java:global/Example/ExampleRemote!com.ibm.example.ExampleRemoteInterface

      

UPDATE
In case of the same app, it also works for me @EJB

without searching.

See also more details:

+2


source







All Articles