为什么EJB是null?

时间:2014-04-28 13:11:16

标签: java ejb ejb-3.1

我正在研究休息服务并同时学习EJB。我只用简单的链控制器运行我的应用程序 - >服务 - > DAO和我的EJB hotelService没有注入 - 它是null。

@Path("/hotels")
public class HotelsController {

@EJB
private HotelService hotelService;


@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getHotelsList(@QueryParam("startDate") String startDate,
                              @QueryParam("endDate") String endDate) {
    List<HotelsEntity> list = hotelService.getAll();
    return ResponseFactory.response(Response.Status.OK, list);
}
}

服务

@Stateless
@EJB(beanInterface = HotelService.class, name = "HotelService")
public class HotelService {

@EJB
private HotelDAO hotelDAO;

public List<HotelsEntity> getAll() {
    return hotelDAO.getAll();
}

public Hotel getHotelById(final String id) {
    return hotelDAO.getHotelById(id);
}
}

DAO

@Stateless
public class HotelDAO {

@PersistenceContext(unitName = Constants.PERSISTENCE_UNIT)
private EntityManager em;

public List<HotelsEntity> getAll() {
    // TODO complete me
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<HotelsEntity> criteria = builder.createQuery(HotelsEntity.class);
    Root<HotelsEntity> root = criteria.from(HotelsEntity.class);
    criteria.select(root);
    TypedQuery<HotelsEntity> resultQuery = em.createQuery(criteria);
    return resultQuery.getResultList();
}

我认为这个问题可能在HotelDAO的实施过程中出现了一些问题,但即使没有它,酒店服务也是空的。

你有什么意见可以解决问题吗?

我使用glassfish 4.0我的pom是:

  <modelVersion>4.0.0</modelVersion>
<groupId>HospitalityRestServer</groupId>
<artifactId>HospitalityRestServer</artifactId>
<packaging>war</packaging>

<version>1.0-SNAPSHOT</version>
<name>HospitalityRestServer Maven Webapp</name>
<url>http://maven.apache.org</url>

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.5.Final</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>annotations-api</artifactId>
        <version>6.0.29</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.ejb</artifactId>
        <version>3.1</version>
    </dependency>
</dependencies>

日志:

[2014-04-28T17:58:05.828+0400] [glassfish 4.0] [WARNING] [] [javax.enterprise.web] 
tid: _ThreadID=22 _ThreadName=http-listener-1(4)] [timeMillis: 1398693485828] [levelValue: 900] [[

StandardWrapperValve [javax.ws.rs.core.Application]:servlet javax.ws.rs.core.Application的Servlet.service()抛出异常 显示java.lang.NullPointerException     at com.example.controller.HotelsController.getHotelsList(HotelsController.java:29)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:606)     at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory $ 1.invoke(ResourceMethodInvocationHandlerFactory.java:81)     在org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:125)     at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider $ ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)     at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:91)     在org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:346)     在org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:341)     在org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:101)     在org.glassfish.jersey.server.ServerRuntime $ 1.run(ServerRuntime.java:224)     在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:271)     在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:267)     在org.glassfish.jersey.internal.Errors.process(Errors.java:315)     在org.glassfish.jersey.internal.Errors.process(Errors.java:297)     在org.glassfish.jersey.internal.Errors.process(Errors.java:267)     在org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)     在org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)     在org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)     在org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)     在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)     在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)     在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)     在org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)     在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)     在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)     在com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)     在org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)     在com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)     在org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)     在org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)     在org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)     在org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119)     在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)     在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)     在org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)     在org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)     在org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)     在org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)     在org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)     在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)     在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:55)     在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)     在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:564)     在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:544)     在java.lang.Thread.run(Thread.java:745) ]

1 个答案:

答案 0 :(得分:4)

您需要实现一个@Local带注释的接口来表示该EJB bean的本地视图,或者您需要将@LocalBean注释添加到HotelService类本身以明确声明它是本地无接口视图那个EJB。

@Stateless
@EJB(beanInterface = HotelService.class, name = "HotelService")
@LocalBean
public class HotelService {
    // ...
}

如果您希望服务在容器外部可用于RMI或EJB客户端执行,则需要创建该服务的@Local和@Remote视图并实现它们。