作为@Stateless EJB 的 JAX-RS 服务:NameNotFoundException

JAX-RS service as @Stateless EJB : NameNotFoundException

我尝试使用 Rest 服务和 EJB 注入构建 Java EE 7 应用程序。 我创建了一个部署在 Glassfish 4 上的多模块 Maven 项目。我的最终 EAR 包含一个带有我的 EJB 的 JAR,例如我的 Rest 服务定义:

@Stateless
@Path("countries")
public class CountryRest {

   //@EJB 
   //StockService stockService;

   @GET
   public Response getCountries() {
      //stockService.getAll(); --> firing a NPE, stockService is Null
      return Response.ok().build();
   }
}

@Stateless
@Remote(IStockService.class)
public class StockService implements IStockService {

    @Override
    public List<Stock> getAllStock() {
        return new ArrayList<Stock>();
    }
}

当我部署我的应用程序时,我看到以下日志,看起来不错。即使我想知道为什么它定义 "java:global" JNDI,因为默认情况下 @Stateless EJB 是 @Local :

Portable JNDI names for EJB CountryRest: [java:global/GeoData-ear/GeoData-ejb-1.0-SNAPSHOT/CountryRest, java:global/GeoData-ear/GeoData-ejb-1.0-SNAPSHOT/CountryRest!com.tomahim.geodata.rest.CountryRest]
Portable JNDI names for EJB StockService: [java:global/GeoData-ear/GeoData-ejb-1.0-SNAPSHOT/StockService, java:global/GeoData-ear/GeoData-ejb-1.0-SNAPSHOT/StockService!com.tomahim.geodata.services.IStockService]

然后当我在 /rest/countries 上执行 GET 时,状态如预期的那样为 200,但我有一个 NameNotFoundException / NamingException:

Avertissement: An instance of EJB class, com.tomahim.geodata.rest.CountryRest, could not be looked up using simple form name. Attempting to look up using the fully-qualified form name.
javax.naming.NamingException: Lookup failed for 'java:module/CountryRest' in 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.naming} [Root exception is javax.naming.NameNotFoundException: No object bound to name java:module/CountryRest]

我看到 "java:module/ContryRest" 的查找与 "java:global/.../CountryRest" 不匹配,但我做错了什么?

编辑 1:通过将 Rest 定义和 EJB 作为 WAR 部署在我的 webapp maven 模块中,我能够使 @Ejb 注入工作。所以似乎只有当我在 JAR 中部署我的 EJB 时才会出现问题。任何想法 ? JAR 和 WAR 部署之间有什么区别?

JAX-RS 规范要求所有 REST 端点必须位于您的 WAR 文件中。您真的需要 EAR 文件吗?