Java Liberty配置文件未在servlet中注入ejb
我在谷歌上搜索,试图理解为什么Liberty Profile 8.5.5.6没有将EJB注入servlet 我把一切都投入了战争。war部署得很好,但当我调用servlet时,会出现以下错误:Java Liberty配置文件未在servlet中注入ejb,java,jakarta-ee,servlets,websphere-liberty,Java,Jakarta Ee,Servlets,Websphere Liberty,我在谷歌上搜索,试图理解为什么Liberty Profile 8.5.5.6没有将EJB注入servlet 我把一切都投入了战争。war部署得很好,但当我调用servlet时,会出现以下错误: Exception thrown by application class 'org.javaee7.servlet.simple.UserServlet.doGet:23' java.lang.NullPointerException: at org.javaee7.servlet.simple.Use
Exception thrown by application class 'org.javaee7.servlet.simple.UserServlet.doGet:23'
java.lang.NullPointerException:
at org.javaee7.servlet.simple.UserServlet.doGet(UserServlet.java:23)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1285)
at [internal classes]
在链接上,我看到一条评论,我应该使用@ManagedBean
当我尝试此操作时,空指针异常消失,但现在出现以下错误:
Error 404: javax.servlet.UnavailableException: SRVE0319E: For the [UserServlet] servlet, org.javaee7.servlet.simple.UserServlet servlet class was found, but a resource injection failure has occurred. CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/org.javaee7.servlet.simple.UserServlet/service reference. The exception message was: The EJB reference in the lps.war module of the lps application could not be resolved; nested exception is: com.ibm.ejs.container.EJBNotFoundException: EJB with interface org.javaee7.service.UserService not present in application lps.
这是我的密码:
@Stateless
public class UserServiceImpl implements UserService {
public User getUser(Long id) {
return new User(id, "Gary");
}
}
@Remote
public interface UserService {
public User getUser(Long id);
}
@ManagedBean
public class UserServlet extends HttpServlet {
@EJB(mappedName = "UserServiceImpl")
UserService service;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = service.getUser(1L);
response.getWriter().print(user == null ? "no user" : user.toString());
}
}
首先-当然不应该使用
@ManagedBean
。Servlet不是托管bean。你不需要注射它来工作。
您应该在servlet中包含@WebServlet
,或者通过web.xml
对其进行配置第二,您应该删除
mappedName
,因为Liberty没有使用它
请在问题中添加server.xml
,因为您可能缺少一些功能。除其他功能外,您应该具有
ejbRemote-3.2
或javaee-7.0
功能
对于远程接口,必须使用以下绑定:
@EJB(lookup="java:global/AppName/ModuleName/UserServiceImpl!org.javaee7.service.UserService")
private UserService service;
根据您的配置,您还可以使用其他绑定,如下所示:
java:global/ExampleApp/ExampleModule/ExampleBean!com.ibm.example.ExampleRemoteInterface
java:app/ExampleModule/ExampleBean!com.ibm.example.ExampleRemoteInterface
java:module/ExampleBean!com.ibm.example.ExampleRemoteInterface
当bean绑定时,您应该在日志中看到以下消息:
[INFO ] CNTR0167I: The server is binding the com.ibm.example.ExampleRemoteInterface interface of the ExampleRemote enterprise bean in the Example.war module of the Example application.
The binding location is: java:global/Example/ExampleRemote!com.ibm.example.ExampleRemoteInterface
更新如果是同一个应用程序,只需
@EJB
,无需任何查找,对我来说也适用
有关更多详细信息,请参见:
- 第5.2.3章在中使用远程EJB开发应用程序
- 信息中心
java:global/lps/userserviceinpl!org.javaee7.service.UserService
我一删除web.xml它就工作了。看起来如果您使用注释(@WebServlet),您应该坚持使用它们。