无法在Rest Web服务中调用EJB

时间:2012-09-07 16:04:33

标签: java rest java-ee dependency-injection ejb

我有一个休息网络服务。在这个休息Web服务中,我使用@EJB来注入EJB。然后我想在我的EJB上调用一个函数。问题是,当我这样做时,整个事情就会爆发。我创建了一个简单的示例来演示。

我有两个项目。一个是包含我的EJB的ear项目。


import javax.ejb.Stateless;
import javax.ejb.LocalBean;

/**
 *
 * @author dan
 */
@Stateless
@LocalBean
public class DummyBean {

    public String dummMessage(){
        return "Hello World";
    }
}

另一个是包含我的网络服务的战争。


@Stateless
@Path("resources")
public class CasperMessage {

    private final Logger logger = Logger.getLogger(ServerMessageManager.class.getName());

    @Context
    private UriInfo context;

    @EJB
    DummyBean db;

    /**
     * Creates a new instance of CasperMessage
     */
    public CasperMessage() {
    }

    @PUT
    @Consumes("application/xml")
    @Produces("application/xml")
    @Path("/message/get")
    public String getMessage(String content) 
    {
       //Message retVal = serverMessageManager.getMessageRecieved(message);
       String retVal = db.dummMessage();
       return retVal;
    };

}

运行代码时,出现以下异常:


SEVERE: EJB5070: Exception creating stateless session bean : [DummyBean]
WARNING: EJB5184:A system exception occurred during an invocation on EJB DummyBean, method: public java.lang.String com.dv.business.messaging.server.DummyBean.dummMessage()
WARNING: javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
    at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:454)
    at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2547)
    at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1899)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at $Proxy207.dummMessage(Unknown Source)
    at com.dv.business.messaging.server.__EJB31_Generated__DummyBean__Intf____Bean__.dummMessage(Unknown Source)
    at com.dv.ws.CasperMessage.getMessage(CasperMessage.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

有任何想法或建议吗?这似乎是一个简单的想法,但它对我不起作用。

1 个答案:

答案 0 :(得分:1)

我和你的班级一起创造简单的战争 - 一切运作良好 WAR 结构

-META-INF/MANIFEST.MF  
-WEB-INF/sun-web.xml  
-WEB-INF/web.xml  
-WEB-INF/classes/com/package/CasperMessage.class  
-WEB-INF/classes/com/package/DummyBean.class  
-index.jsp  

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="3.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>  

sun-web.xml

<sun-web-app>
    <context-root>/api</context-root>
</sun-web-app>  

部署在 Glass Fish 3.1 上,在 / api / resources / message / get 上发送PUT请求,获得响应