Home > Back-end >  Stateless Bean is null
Stateless Bean is null

Time:12-17

It was working earlier when I was jersey 1.1. But now it says this.userBean is null. Apart from that there is one warning in the log related com.sun.faces.flow.FlowDiscoveryCDIHelper is deprecated from CDI 1.1!

logs:

08:30:45,161 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-4) WFLYSRV0027: Starting deployment of "ejb2.war" (runtime-name: "ejb2.war")
08:30:45,753 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) WFLYWELD0003: Processing weld deployment ejb2.war
08:30:45,783 INFO  [org.jboss.as.ejb3.deployment] (MSC service thread 1-3) WFLYEJB0473: JNDI bindings for session bean named 'UserSessionBean' in deployment unit 'deployment "ejb2.war"' are as follows:

        java:global/ejb2/UserSessionBean!com.enovate.assignment.ejb2.UserSessionBeanLocal
        java:app/ejb2/UserSessionBean!com.enovate.assignment.ejb2.UserSessionBeanLocal
        java:module/UserSessionBean!com.enovate.assignment.ejb2.UserSessionBeanLocal
        java:global/ejb2/UserSessionBean
        java:app/ejb2/UserSessionBean
        java:module/UserSessionBean

08:30:45,845 INFO  [io.jaegertracing.internal.JaegerTracer] (MSC service thread 1-3) No shutdown hook registered: Please call close() manually on application shutdown.
08:30:45,999 INFO  [io.smallrye.metrics] (MSC service thread 1-3) MicroProfile: Metrics activated (SmallRye Metrics version: 2.4.2)
08:30:46,008 WARN  [org.jboss.weld.Bootstrap] (MSC service thread 1-3) WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class com.sun.faces.flow.FlowDiscoveryCDIHelper is deprecated from CDI 1.1!
08:30:46,210 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 126) Initializing Mojarra 2.3.14.SP01 for context '/ejb2'
08:30:47,353 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 126) WFLYUT0021: Registered web context: '/ejb2' for server 'default-server'
08:30:47,440 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0016: Replaced deployment "ejb2.war" with deployment "ejb2.war"
08:30:47,448 INFO  [org.jboss.as.repository] (DeploymentScanner-threads - 2) WFLYDR0002: Content removed from location C:\Users\teoti\Desktop\office\wildfly-21.0.2.Final\standalone\data\content\15\e071e0f42a2e0339da8b4d636a6496c5b1146e\content
08:33:05,177 ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to /ejb2/webapi/register: javax.servlet.ServletException: java.lang.NullPointerException: Cannot invoke "com.enovate.assignment.ejb2.UserSessionBeanLocal.isUserPresent(String)" because "this.userBean" is null

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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
    <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.ejb2</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

services:

import javax.ejb.EJB;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("")
public class Service  
{
    @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.isUserPresent(email)) 
            return Response.ok("Email already registered!!").build();
        userBean.addUser(new User(name, email, pass));
        return Response.ok("Registered!!").build();
    }
}

UserSessionBean:

import java.util.ArrayList;

import javax.ejb.Stateless; 

@Stateless
public class UserSessionBean implements UserSessionBeanLocal
{
    static ArrayList<User> usersList = new ArrayList<User>();
    static int userCount = 0;
    User u = null;
    
    public boolean isUserPresent(final String email) 
    {
        return usersList.stream().anyMatch(d -> d.getEmail().equals(email));
    } 
    
    public void addUser(User newUser) 
    {
        usersList.add(newUser);
        userCount  ;
    } 
}

User:

public class User 
{
    private String name;
    private String password;
    private String email;   
    
    User(final String name, final String email, final String password) 
    {
        this.name = name;
        this.email = email;
        this.password = password;
    }
    
...
}

I thought it would work fine but I don't know how it is null. Maybe is related to CDI warning of log or something related to newer version of jersey or @EJB not working. I also tried adding beans.xml file.

Any help is appreciated

CodePudding user response:

The reason was something related to Non-managed bean. And I can not use @EJB to initialize the bean, I have to use Jndi lookup way like this inside function:

@POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("register")
    public Response register(@FormParam("userName") final String name, @FormParam("userPassword") final String pass, @FormParam("userEmail") String email) 
    {
        UserSessionBeanLocal userBean = null;
        try 
        {
            InitialContext ic = new InitialContext();
            userBean = (UserSessionBeanLocal)ic.lookup("java:global/ejb/UserSessionBean!com.demo.ejb.UserSessionBeanLocal");
        } catch (NamingException e) 
        {
            e.printStackTrace();
        }
...

CodePudding user response:

If you only want to use @EJB this might work:

I was using jersey in the project and I feels like there is some problem with jersey libraries.

  1. So I deleted all the dependency inside the dependecies below build from the pom.xml and update the project
  2. Added to javaee-api-8.0.jar file to the classpath
  3. Clear the everything inside the web-app (everything)
  4. Added root resource file by extending javax.ws.rs.core.Application like here and it worked
  • Related