@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.