gpt4 book ai didi

java - 如何在 java ee 6 框架中的 JAX-WS Web 服务中保存数据?

转载 作者:行者123 更新时间:2023-12-01 14:04:18 25 4
gpt4 key购买 nike

我有一个使用 java ee 6 框架的 J2EE 项目。带有 EJB 和 War 模块。我想向EJB模块添加一个Service来操作数据库中的一些数据,所以我需要访问Service方法中的DAO方法。但它返回错误!

@WebService(serviceName = "AddUserService")
@Stateless()
public class AddUserService {
@PersistenceContext(unitName = "UserRegistrationService-ejbPU")
EntityManager em;

@WebMethod(operationName = "registerUser")
public String registerUser(BasicUserInfo basicUserInfo) {
UserEJB userEJB=new UserEJB(em);
String retValue = "";
User user = new User();
user.setAge(basicUserInfo.getAge());
user.setUserName(userName);
userEJB.addUser(user);
retValue = " User Registered with user_ID:" + user.getId().toString() + " and userName:" + user.getUserName();
return retValue;
}
}

EJB 类代码是:

    @TransactionManagement(TransactionManagementType.CONTAINER)
@Stateless
public class UserEJB {

@PersistenceContext(unitName = "UserRegistrationService-ejbPU")
EntityManager em;

public UserEJB() {
}

public UserEJB(EntityManager em) {
this.em = em;
}

public void addUser(User user) {
em.persist(user);
}
}

测试服务会在服务器上出现此错误:

        at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at dao.UserEJB.addUser(UserEJB.java:34)
at app.Services.AddUserService.registerUser2(AddUserService.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)

最佳答案

您的 EntityManger 不会被容器注入(inject),因为您自己创建了 UserEJB 对象!你已经破坏了注入(inject)链。使用 DI 时,您必须让容器为您创建对象。试试这个方法:

@WebService(serviceName = "AddUserService")
@Stateless()
public class AddUserService {

//actually you don't need this here, you'll get that injected inside the UserEJB
@PersistenceContext(unitName = "UserRegistrationService-ejbPU")
EntityManager em;


@Inject
UserEJB userEjb;

@WebMethod(operationName = "registerUser")
public String registerUser(BasicUserInfo basicUserInfo) {
//you don't need this line, the container will instantiate the object for you!
//UserEJB userEJB=new UserEJB(em);
String retValue = "";
User user = new User();
user.setAge(basicUserInfo.getAge());
user.setUserName(userName);
userEJB.addUser(user);
retValue = " User Registered with user_ID:" + user.getId().toString() + " and userName:" + user.getUserName();
return retValue;
}
}

关于java - 如何在 java ee 6 框架中的 JAX-WS Web 服务中保存数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19049951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com