简体   繁体   English

无状态 Bean 为空

[英]Stateless Bean is null

It was working earlier when I was jersey 1.1.当我是 jersey 1.1 时,它工作得更早。 But now it says this.userBean is null .但现在它说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!除此之外,日志中有一个警告,与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:

<?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:用户会话Bean:

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.可能与日志的 CDI 警告或与较新版本的球衣或@EJB 不工作有关。 I also tried adding beans.xml file.我还尝试添加 beans.xml 文件。

Any help is appreciated任何帮助表示赞赏

The reason was something related to Non-managed bean.原因与非托管 bean 有关。 And I can not use @EJB to initialize the bean, I have to use Jndi lookup way like this inside function:而且我不能使用 @EJB 来初始化 bean,我必须在函数内部使用这样的 Jndi 查找方式:

@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();
        }
...

If you only want to use @EJB this might work:如果您只想使用 @EJB 这可能有效:

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所以我从pom.xml中删除了下面构建的依赖项中的所有依赖并更新了项目
  2. Added to javaee-api-8.0.jar file to the classpath添加到javaee-api-8.0.jar文件到类路径
  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通过像这里这样扩展javax.ws.rs.core.Application添加根资源文件并且它有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM