这是工作早,当我是球衣1.1。但现在是this.userBean is null
。除此之外,在与com.sun.faces.flow.FlowDiscoveryCDIHelper is deprecated from CDI 1.1!
相关的日志中有一个警告
日志:
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:UsersteotiDesktopofficewildfly-21.0.2.Finalstandalonedatacontent15e071e0f42a2e0339da8b4d636a6496c5b1146econtent
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>
服务:
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++;
}
}
用户:
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;
}
...
}
我认为它会工作得很好,但我不知道它是空的。可能与日志的CDI警告有关,或者与新版本的jersey或@EJB不工作有关。我还尝试添加beans.xml文件。
感谢您的帮助
原因与非托管bean有关。并且我不能使用@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();
}
...
如果您只想使用@EJB,这可能可行:
我在项目中使用jersey,我觉得jersey库有一些问题。
- 所以我删除了所有的依赖依赖下面从构建pom.xml更新项目 添加到javaee-api-8.0.jar文件到类路径
- 清除web-app中的所有内容(一切)
- 通过扩展javax.ws.rs.core.Application添加根资源文件就像这里,它工作了