EJB没有被注入JAX-RS

时间:2014-03-08 10:34:40

标签: jboss jax-rs cdi

我正在使用JBoss 6.2.0 EAP,我试图将我的EJB注入到我的JAX-RS类中,但是我得到了一个N​​ullPointerEXception。它绝对是CUstomerDAO类,因为第29行的错误是在null对象上调用printOut方法。我在我的WEB-INF中添加了beans.xml。我想知道我添加的servlet,或者我添加的web.xml配置是否给了我一个错误。我在jboss服务器上声明了一个数据源,并且工作正常。这只是我添加的新东西似乎抛出错误。如果有人想要这个项目,我可以把它搞定。

这是我的文件

错误堆栈跟踪

10:11:32,834 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "mysql-connector-java-5.1.29-bin.jar" (runtime-name: "mysql-connector-java-5.1.29-bin.jar")
10:11:32,835 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-4) JBAS015012: Started FileSystemDeploymentService for directory /opt/jboss-eap-6.2/standalone/deployments
10:11:33,508 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-1) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)
10:11:33,536 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-4) JBAS010400: Bound data source [java:/tutorial]
10:11:33,769 INFO  [org.jboss.as.server] (Controller Boot Thread) JBAS018559: Deployed "mysql-connector-java-5.1.29-bin.jar" (runtime-name : "mysql-connector-java-5.1.29-bin.jar")
10:11:33,852 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
10:11:33,852 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
10:11:33,853 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss EAP 6.2.0.GA (AS 7.3.0.Final-redhat-14) started in 6615ms - Started 160 of 220 services (58 services are passive or on-demand)
10:12:44,006 INFO  [org.jboss.as.repository] (HttpManagementService-threads - 2) JBAS014900: Content added at location /opt/jboss-eap-6.2/standalone/data/content/bc/c99f85aabe406819b2f1f6d5c660b3fcb0d231/content
10:12:51,778 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015876: Starting deployment of "webproject.war" (runtime-name: "webproject.war")
10:12:52,031 WARN  [org.jboss.weld.deployer] (MSC service thread 1-2) JBAS016010: Warning while parsing vfs:/content/webproject.war/WEB-INF/beans.xml:6 SchemaLocation: schemaLocation value = '              http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd' must have even number of URI's.
10:12:52,103 INFO  [org.jboss.as.jpa] (MSC service thread 1-2) JBAS011401: Read persistence.xml for tutorial
10:12:52,239 INFO  [org.jboss.weld.deployer] (MSC service thread 1-2) JBAS016002: Processing weld deployment webproject.war
10:12:52,267 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-2) JNDI bindings for session bean named WebProject in deployment unit deployment "webproject.war" are as follows:

    java:global/webproject/WebProject!com.example.ejbrest.WebProject
    java:app/webproject/WebProject!com.example.ejbrest.WebProject
    java:module/WebProject!com.example.ejbrest.WebProject
    java:global/webproject/WebProject
    java:app/webproject/WebProject
    java:module/WebProject

10:12:52,268 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-2) JNDI bindings for session bean named CustomerDAO in deployment unit deployment "webproject.war" are as follows:

    java:global/webproject/CustomerDAO!com.example.ejbrest.CustomerDAO
    java:app/webproject/CustomerDAO!com.example.ejbrest.CustomerDAO
    java:module/CustomerDAO!com.example.ejbrest.CustomerDAO
    java:global/webproject/CustomerDAO
    java:app/webproject/CustomerDAO
    java:module/CustomerDAO

10:12:52,270 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-2) JNDI bindings for session bean named CustomerWS in deployment unit deployment "webproject.war" are as follows:

    java:global/webproject/CustomerWS!com.example.ejbrest.CustomerWS
    java:app/webproject/CustomerWS!com.example.ejbrest.CustomerWS
    java:module/CustomerWS!com.example.ejbrest.CustomerWS
    java:global/webproject/CustomerWS
    java:app/webproject/CustomerWS
    java:module/CustomerWS

10:12:52,514 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) JBAS016005: Starting Services for CDI deployment: webproject.war
10:12:52,598 INFO  [org.jboss.weld.Version] (MSC service thread 1-3) WELD-000900 1.1.16 (redhat)
10:12:52,657 INFO  [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016008: Starting weld service for deployment webproject.war
10:12:52,658 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 48) JBAS011402: Starting Persistence Unit Service 'webproject.war#tutorial'
10:12:52,879 INFO  [org.hibernate.annotations.common.Version] (ServerService Thread Pool -- 48) HCANN000001: Hibernate Commons Annotations {4.0.1.Final-redhat-2}
10:12:52,897 INFO  [org.hibernate.Version] (ServerService Thread Pool -- 48) HHH000412: Hibernate Core {4.2.7.SP1-redhat-3}
10:12:52,900 INFO  [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 48) HHH000206: hibernate.properties not found
10:12:52,912 INFO  [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 48) HHH000021: Bytecode provider name : javassist
10:12:52,937 INFO  [org.hibernate.ejb.Ejb3Configuration] (ServerService Thread Pool -- 48) HHH000204: Processing PersistenceUnitInfo [
    name: tutorial
    ...]
10:12:53,042 INFO  [org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator] (ServerService Thread Pool -- 48) HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
10:12:53,377 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 48) HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
10:12:53,496 INFO  [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] (ServerService Thread Pool -- 48) HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory
10:12:53,502 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (ServerService Thread Pool -- 48) HHH000397: Using ASTQueryTranslatorFactory
10:12:53,563 INFO  [org.hibernate.validator.internal.util.Version] (ServerService Thread Pool -- 48) HV000001: Hibernate Validator 4.3.1.Final-redhat-1
10:12:54,400 INFO  [org.jboss.web] (ServerService Thread Pool -- 49) JBAS018210: Register web context: /webproject
10:12:54,476 INFO  [org.jboss.resteasy.cdi.CdiInjectorFactory] (ServerService Thread Pool -- 49) Found BeanManager at java:comp/BeanManager
10:12:55,057 INFO  [org.jboss.as.server] (HttpManagementService-threads - 2) JBAS018559: Deployed "webproject.war" (runtime-name : "webproject.war")
10:13:08,430 INFO  [org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher] (http-localhost/127.0.0.1:8080-1) Deploying javax.ws.rs.core.Application: class com.example.ejbrest.RestApplication$Proxy$_$$_WeldClientProxy
10:13:08,441 INFO  [org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher] (http-localhost/127.0.0.1:8080-1) Adding singleton resource com.example.ejbrest.CustomerWS from Application javax.ws.rs.core.Application
10:13:08,649 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/webproject].[Resteasy]] (http-localhost/127.0.0.1:8080-1) JBWEB000236: Servlet.service() for servlet Resteasy threw exception: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
    at org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:365) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:233) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:209) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:557) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:524) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_51]


Caused by: java.lang.NullPointerException
    at com.example.ejbrest.CustomerWS.getCustomer(CustomerWS.java:29) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_51]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_51]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_51]
    at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_51]
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:269) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:227) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:216) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:542) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
    ... 21 more

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <display-name>RestEasy sample Web Application</display-name>
  <listener>
    <listener-class>
            org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
        </listener-class>
  </listener>

  <servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>

    <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>com.example.ejbrest.RestApplication</param-value>
    </init-param>
    <!--  <load-on-startup>1</load-on-startup>-->
  </servlet>

  <servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/customers/*</url-pattern>
  </servlet-mapping>



  <servlet>
    <servlet-name>ControllerServlet</servlet-name>
    <servlet-class>com.example.ejbrest.ControllerServlet</servlet-class>
    <!--  <load-on-startup>0</load-on-startup>-->
  </servlet>
 <servlet-mapping>
    <servlet-name>ControllerServlet</servlet-name>
    <!--  <url-pattern>/tutorial/*</url-pattern>-->
    <url-pattern>/application/*</url-pattern>
  </servlet-mapping> 

</web-app>

的beans.xml

<!-- Marker file indicating CDI should be enabled -->
<beans xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="

      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd">
</beans>
<!--  http://java.sun.com/xml/ns/javaee/beans_1_0.xsd" 
http://java.sun.com/xml/ns/javaee --> 

的persistence.xml :

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">

    <persistence-unit name="tutorial">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/tutorial</jta-data-source>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>

</persistence>

Java类:

package com.example.ejbrest;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import org.jboss.resteasy.plugins.providers.html.View;

@Path("/customers")
@Stateless
@LocalBean
public class CustomerWS {


    @EJB
    private CustomerDAO customersDao;

    @GET
    @Path("/first/{id}")
    public Customer getCustomer(@PathParam("id") int id) {

        customersDao.printOut();

        return customersDao.getCustomer(id);
    }


    @GET
    @Path("/second/{id}")
    public View getCustomer() {
        System.out.println("Got in here! ! ! ");
        View v = new View("/application");
        //v.render(arg0, arg1);
        //System.out.println("path " + v.getPath());
        return v;
    }

    @POST
    public void addCustomers(List<Customer> customers) {
        customersDao.addCustomers(customers);
    }


}

CustomerDAO:

package com.example.ejbrest;

import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
@LocalBean
public class CustomerDAO {

    @PersistenceContext
    private EntityManager em;

    public Customer getCustomer(int id) {
        return em.find(Customer.class, id);
    }

    public void printOut(){
        System.out.println("AGGGGGH!");
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void addCustomers(List<Customer> customers) {
        for (Customer customer : customers) {
            em.persist(customer);
        }
    }

}

CustomerWS

package com.example.ejbrest;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import org.jboss.resteasy.plugins.providers.html.View;

@Path("/customers")
@Stateless
@LocalBean
public class CustomerWS {


    @EJB
    private CustomerDAO customersDao;

    @GET
    @Path("/first/{id}")
    public Customer getCustomer(@PathParam("id") int id) {

        customersDao.printOut();

        return customersDao.getCustomer(id);
    }


    @GET
    @Path("/second/{id}")
    public View getCustomer() {
        System.out.println("Got in here! ! ! ");
        View v = new View("/application");
        //v.render(arg0, arg1);
        //System.out.println("path " + v.getPath());
        return v;
    }

    @POST
    public void addCustomers(List<Customer> customers) {
        customersDao.addCustomers(customers);
    }


}

目录结构

enter image description here

2 个答案:

答案 0 :(得分:0)

我看到一些问题,其中一些可能是红色的鲱鱼所以我想我会把它们全部列出来。

请勿在{{1​​}}中包含所有此重新配置。如果没有别的,它可能会混淆应用服务器。您应该有一个如下所示的JAX-RS应用程序:

web.xml

这就是所需要的。从配置中删除resteasy listener和servlet。

您正在使用CDI 1.1 @ApplicationPath("/customers") public class RestApplication extends Application { // nothing needed ... } 。 EAP 6.2符合CDI 1.0标准。将文件设为空或在文件中使用CDI 1.0标记。

你实际上有两个不同的beans.xml类吗?或者是复制粘贴问题?

答案 1 :(得分:0)

首先是您的代码的几个注释:

  1. 您不需要项目中的beans.xml文件,因为使用不使用CDI注释而只使用EJB3注释
  2. @LocalBean注释不是必需的,@ Stateless足以定义本地bean,详细解释可以在EJB 3.1 @LocalBean vs no annotation
  3. 中找到
  4. 不需要在web.xml中为resteasy定义监听器,servlet和servlet映射元素,JBoss / RestEasy(JAX-RS实现库)会自动为您添加这些定义。使用root web-app元素的空web.xml就足够了。
  5. 现在你的问题: 属性CustomerWS.customersDao不是由EJB容器注入的,因为CustomerWS不是按容器部署为EJB bean,而是仅作为RestEasy的JAX-RS资源。在JBoss EAP 6.x中使用RestEasy 2.x的已知缺点是,restfull资源无法成为开箱即用的EJB bean:https://docs.jboss.org/resteasy/docs/2.3.6.Final/userguide/html/RESTEasy_EJB_Integration.html

    您需要做的就是在web.xml中定义特殊的上下文参数,使用逗号分隔的EJB bean的jndi名称,这些名称也是RestEasy管理的restfull资源。因此,在您的情况下,您需要在以下代码段中插入web.xml:

    <context-param>
        <param-name>resteasy.jndi.resources</param-name>
        <param-value>java:module/CustomerWS</param-value>
    </context-param>
    

    就是这样。现在,CustomerWS类将作为EJB bean进行处理,并进行注入。