【问题标题】:Unable to run ejb application when interface (local and remote ) is added添加接口(本地和远程)时无法运行 ejb 应用程序
【发布时间】:2015-12-27 00:48:30
【问题描述】:

使用环境

Netbeans 8.0+

玻璃鱼 4+

jdk 1.8

请注意,如果您不使用界面视图,则此示例效果很好。如果我们使用本地或远程界面,则效果不佳

远程界面

package packt;
import javax.ejb.Remote;
@Remote
public interface SphereBeanRemote {
     public double computeVolume(double radius);
}

上述接口的实现

package packt;
import javax.ejb.Stateless;
@Stateless
public class SphereBean implements SphereBeanRemote {
    @Override
    public double computeVolume(double radius) {
         return (4.0/3.0)*Math.PI*(radius*radius*radius);
    }

}

访问它的客户端(servlet)

import java.io.*;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import packt.SphereBean;

@WebServlet(urlPatterns = {"/SphereServlet"})
public class SphereServlet extends HttpServlet {
    @EJB
    SphereBean sphere;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {

            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SphereServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet SphereServlet at " + request.getContextPath() + "</h1>");
            out.println("<h1>" + sphere.computeVolume(18)  + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

      @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
      @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}

错误 HTTP 状态 500 - 内部服务器错误

输入异常报告

消息内部服务器错误

描述服务器遇到了一个内部错误,导致它无法完成这个请求。

异常

javax.servlet.ServletException: 实例化 servlet 类 SphereServlet 时出错

根本原因

com.sun.enterprise.container.common.spi.util.InjectionException:为类创建托管对象时出错:类 SphereServlet

根本原因

com.sun.enterprise.container.common.spi.util.InjectionException: 尝试注入远程 ejb-ref name=SphereServlet/sphere,Remote 3.x interface =packt.SphereBean,ejb-link=null,lookup 的异常=,mappedName=,jndi-name=packt.SphereBean,refType=Session 进入类 SphereServlet:在 SerialContext [myEnv={java.naming.factory.initial=com. sun.enterprise.naming.impl.SerialInitContextFactory,java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,java.naming.factory.url.pkgs=com.sun.enterprise。命名}

根本原因

javax.naming.NamingException:在 SerialContext [myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java 中查找“java:comp/env/SphereServlet/sphere”失败.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [根异常是 javax.naming.NamingException :为“远程 ejb-ref name=SphereServlet/sphere,Remote 3.x interface =packt.SphereBean,ejb-link=null,lookup=,mappedName=,jndi-name=packt.SphereBean,refType=Session”解析 Ejb 的异常.用于查找的实际(可能是内部)远程 JNDI 名称是“packt.SphereBean#packt.SphereBean”[根异常是 javax.naming.NamingException:在 SerialContext[myEnv={java] 中查找“packt.SphereBean#packt.SphereBean”失败.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url .pkgs=com.sun.enterprise.naming} [根异常是 javax.naming.NameNotFoundException: packt.SphereBean#packt.SphereBean not found]]]

根本原因

javax.naming.NamingException: 为 'Remote ejb-ref name=SphereServlet/sphere,Remote 3.x interface =packt.SphereBean,ejb-link=null,lookup=,mappedName=,jndi-name= 解析 Ejb 异常packt.SphereBean,refType=Session' 。用于查找的实际(可能是内部)远程 JNDI 名称是“packt.SphereBean#packt.SphereBean”[根异常是 javax.naming.NamingException:在 SerialContext[myEnv={java] 中查找“packt.SphereBean#packt.SphereBean”失败.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url .pkgs=com.sun.enterprise.naming} [根异常是 javax.naming.NameNotFoundException: packt.SphereBean#packt.SphereBean not found]]

根本原因

javax.naming.NamingException:在 SerialContext [myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming 中查找“packt.SphereBean#packt.SphereBean”失败.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [根异常是 javax.naming.NameNotFoundException: packt .SphereBean#packt.SphereBean 未找到]

根本原因

javax.naming.NameNotFoundException: 未找到 packt.SphereBean#packt.SphereBean

note GlassFish Server Open Source Edition 4.1 日志中提供了异常的完整堆栈跟踪及其根本原因。 GlassFish Server 开源版 4.1

【问题讨论】:

    标签: jakarta-ee glassfish ejb-3.0 ejb-3.1


    【解决方案1】:

    规范的第 4.9.8 节规定:

    以下是公开无接口视图的会话 bean 的要求:

    • bean 类必须通过其 bean 类定义或在部署描述符中指定它公开一个无接口视图。以下规则适用:

      • 如果 bean 没有公开任何其他客户端视图(本地、远程、无接口、2.x 远程主页、2.x 本地主页、Web 服务)并且它的 implements 子句为空,则 bean 定义一个 no-界面视图。

      • 如果 bean 至少公开一个其他客户端视图,则 bean 通过 bean 类或部署描述符中的 LocalBean 注释指定它公开一个无接口视图。

    因此,改变你的实现:

    package packt;
    import javax.ejb.Stateless;
    import javax.ejb.LocalBean;
    
    @LocalBean
    @Stateless
    public class SphereBean implements SphereBeanRemote {
        @Override
        public double computeVolume(double radius) {
             return (4.0/3.0)*Math.PI*(radius*radius*radius);
        }
    }
    

    应该为您解决问题。

    但是,如果您想实际使用 Local/Remote 接口,那么这就是您在注入时应该使用的类型:

    @WebServlet(urlPatterns = {"/SphereServlet"})
    public class SphereServlet extends HttpServlet {
    
        @EJB
        SphereBeanRemote sphere;
    
        ...
    

    【讨论】:

    • 我认为你没有得到我的问题。如果我使用远程或本地接口,我的问题是如何在上面运行应用程序
    • 我已经添加了它。请好好看看它。但我仍然收到错误
    猜你喜欢
    • 2011-04-17
    • 2012-09-13
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    相关资源
    最近更新 更多