0

@EJB not working, returning null pointer exception when used. I am using jersey. It is working in the similar project of jersey earlier.

Rest API class
@Path("")
public class Services 
{   
    @EJB 
    UserSessionBeanLocal userBean;
    
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("register")
    public Response register(@FormParam("userName") final String name, @FormParam("userPassword") final String pass, @FormParam("userEmail") String email) 
    {   
        if (userBean.isEmailRegistered(email))   // userBean is null here
            {
                           // code
                        }
    }
}
Stateless EJB
@Stateless
public class UserSessionBean implements UserSessionBeanLocal
{
    static ArrayList<User> usersList = new ArrayList<User>();
    static int userCount = 0;
    User u = null;

    @Override
    public boolean isEmailRegistered(final String email) 
    {
        return usersList.stream().anyMatch(d -> d.getEmail().equals(email));
    }
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" 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/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.enovate.assignment.ejb1</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/user/*</url-pattern>
    </servlet-mapping>
</web-app>

Any help is appreciated. Thanks in advance.

I was expecting it should work fine. Now I have to use the JNDI lookup way to instantiate the bean. As I mentioned it is working in almost similar project. I found something that @EJB does not work in non-managed bean if is related, but I don't understand it.

  • If you only want to use @EJB, here is Similar question here: [link](https://stackoverflow.com/questions/74622563/stateless-bean-is-null/74822866#74822866:~:text=If%20you%20only%20want%20to%20use%20%40EJB%20this%20might%20work%3A) – Prashant Kumar Dec 16 '22 at 10:31

0 Answers0