【问题标题】:JAX-RS service as @Stateless EJB : NameNotFoundException作为 @Stateless EJB 的 JAX-RS 服务:NameNotFoundException
【发布时间】:2015-01-03 03:50:24
【问题描述】:

我尝试使用 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 放置在我的 webapp maven 模块部署为 WAR,我能够使 @Ejb 注入工作。所以似乎只有当我在 JAR 中部署 EJB 时才会出现问题。任何的想法 ? JAR 和 WAR 部署有什么区别?

【问题讨论】:

    标签: rest glassfish jax-rs ejb-3.0


    【解决方案1】:

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

    【讨论】:

    • 我不知道这个要求,谢谢。我真的不需要 EAR 文件,我只是尝试尝试如何构建具有多个模块的大型 Java EE 应用程序。因此,如果我的 REST 端点需要在 WAR 文件中,我应该将我的其他 EJB 放在这个 WAR 中还是可以将它们放在单独的 JAR 中?
    • 您可以将 JAR 放在 WAR 的 lib 文件夹中。您还可以将其余资源放在 WAR 库中的 JAR 中。您只是不能将它们作为 EJB-JAR 放在 EAR 中。
    猜你喜欢
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多